Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在RegEx对象中使用元字符/w_Javascript_Regex - Fatal编程技术网

Javascript 如何在RegEx对象中使用元字符/w

Javascript 如何在RegEx对象中使用元字符/w,javascript,regex,Javascript,Regex,在下面的示例中,我想测试字符串是否由允许的字符组成。然而,我没有得到预期的结果。我错过了什么 const string = 'sdf^&&*^%^'; const pattern = "\\w"; const allowed = new RegExp(pattern); const pattern2 = "\w"; const allowed2 = new RegExp(pattern); const pattern3 = /\w/g; document.getEleme

在下面的示例中,我想测试字符串是否由允许的字符组成。然而,我没有得到预期的结果。我错过了什么

const string = 'sdf^&&*^%^';

const pattern = "\\w";
const allowed = new RegExp(pattern);

const pattern2 = "\w";
const allowed2 = new RegExp(pattern);

const pattern3 = /\w/g;

document.getElementById("app").innerHTML = `
<p>This should be false (but is true): ${allowed.test(string)}</p>
<p>This should be false (but is true): ${allowed.test(string)}</p>
<p>This should be false (works): ${string.match(pattern3)}</p>
`;
------编辑:

原来前面的解释令人困惑。也许这会把事情弄清楚

在我的代码中,regex模式通过React中的属性传递给子组件。因此,不可能使用正则表达式本身来传递它。因此,问题可能更多:

如何使用字符串中的正则表达式模式来匹配单个单词a-z、a-z、0-9,包括uu下划线字符

测试设置应为:

const allowedCharacters = 'abc_123';
const unallowedCharacters = '*&^#';

const regex = /^\w+$/;
const regexString = "/^\w+$/"; // <- hence the string

// -------

console.log('--- regex without it coming from a string');

const regexObjectWithoutString = new RegExp(regex);

console.log('Should be abc_123: ', allowedCharacters.match(regex)); // ["abc_123"]
console.log('Should be true: ', regexObjectWithoutString.test(allowedCharacters)); // true
console.log('Should be false', regexObjectWithoutString.test(unallowedCharacters)); // false

// all good until now
// -------

console.log('--- this time regex from a string');

const regexObjectByString = new RegExp(regexString);

console.log('Should be abc_123: ', allowedCharacters.match(regexString)); // null
console.log('Should be true: ', regexObjectByString.test(allowedCharacters)); // false
console.log('Should be false', regexObjectByString.test(unallowedCharacters)); // false

Fiddle:

您的示例字符串包含一些有效字符,因此/\w/表达式将始终找到匹配项。如果希望正则表达式验证它只包含由\w匹配的字符,则表达式应为/^\w+$。从字符串开头开始匹配,在结尾停止匹配,需要1个或多个\w字符


我建议使用这样的工具,在您尝试构建正则表达式时提供正则表达式的可视化。

为什么allowed2和allowed2使用相同的字符串来构建正则表达式?为什么您认为您所说的结果应该是这样的?例如,您使用了\w,它匹配任何单词字符。您说过字符串不应该匹配它,但它应该匹配,因为它的第一个字符是单词字符。如果要查找完全匹配,则需要在表达式中添加锚定。更多和更多。投票结束作为打字错误/不重复/对其他人不有用。需要更多。那个codesandbox页面根本不起作用,包含不同的代码:pattern3完全丢失。@t.J.Crowder不知道如何解释它。添加了第二次尝试,可能会更清晰一些。谢谢@TeCHoward,这让我更接近了解发生了什么。现在这种模式似乎是正确的。我唯一仍然遇到的事情是将字符串传递到正则表达式中。