javascript正则表达式问号(?)未检测到

时间:2022-02-08 22:24:18

Hi all I tried to create some regex with random value.

大家好,我试图用随机值创建一些正则表达式。

var data = "demo purpose?";  **OR**  var data = "demo purpose";
var sentence = "can I put these app as demo purpose?";
var re = new RegExp("\\b(" + data + ")\\b", "g");
console.log(sentence.match(re));   // output ["demo purpose"]

In variable data have two different value demo purpose? & demo purpose with only question mark. Both console out are same please any one Give me hint what should i do in these case.

在可变数据中有两个不同的值演示目的吗?和演示目的只有问号。两个控制台都是相同的请任何一个给我提示我应该在这些情况下做什么。

- Thank you

- 谢谢

3 个解决方案

#1


10  

you need to escape ? (i.e. write \\?) or else it would be interpreted as a quantifier in regex.

你需要逃脱吗? (即写\\?)或者它将被解释为正则表达式中的量词。

Furthermore, the \\b is not really necessary because it tries to match a non blank char in which case there is nothing behind demo purpose? so sentence.match(new RegExp("\\b(demo purpose\\?)\\b", "g")) would return null.

此外,\\ b并不是真的有必要,因为它试图匹配一个非空白字符,在这种情况下,没有什么可用于演示目的?所以sentence.match(新的RegExp(“\\ b(演示目的\\?)\\ b”,“g”))将返回null。

If you want randomness, use Math.random. Make an array and get an random integer or 0 or 1 (with Math.floor) as the index.

如果您想要随机性,请使用Math.random。创建一个数组并获得一个随机整数或0或1(使​​用Math.floor)作为索引。

#2


2  

In order to pass variables into JS regex when using constructor notation, you need to escape all characters that act as regex special characters (quantifiers, group delimiters, etc.).

为了在使用构造函数表示法时将变量传递给JS正则表达式,您需要转义所有充当正则表达式特殊字符的字符(量词,组分隔符等)。

The escape function is available at MDN Web site:

MDN网站上提供了转义功能:

function escapeRegExp(string){
  return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

Note also, that \b is a word boundary, and it prevents from matching the strings you need as ? is a non-word character. If you do not need to match word boundaries, remove \b. If you need to check if the search word is a whole word, use (?:^|\W) at the beginning and (?!\w) at the end and use exec rather than match to obtain access to the captured group 1.

另请注意,\ b是单词边界,它会阻止匹配您需要的字符串?是一个非单词字符。如果您不需要匹配单词边界,请删除\ b。如果你需要检查搜索词是否是整个单词,请在开头使用(?:^ | \ W),在结尾使用(?!\ w)并使用exec而不是匹配来获取对捕获的组1的访问权限。

So, your code will become:

那么,您的代码将变为:

function escapeRegExp(string){
  return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

var data = "demo purpose?";
var sentence = "can I put these app as demo purpose?";
var re = new RegExp("(?:^|\\W)(" + escapeRegExp(data) + ")(?!\\w)", "g");
while ((m = re.exec(sentence)) !== null) {
   console.log(m[1]);   // output ["demo purpose?"]
}

If you search for emo purpose?, no result will be returned since there will be no match.

如果您搜索emo目的?,将不会返回任何结果,因为没有匹配项。

#3


0  

This

var data = "demo purpose?"; // **OR**  var data = "demo purpose";
var sentence = "can I put these app as demo purpose?";
var re = new RegExp(/demo purpose\?/, "g");
console.log(sentence.match(re));   // output ["demo purpose?"]

return ["demo purpose?"], changing RegExp("xxx", "g"); to RegExp(/xxx/, "g");

return [“demo purpose?”],改变RegExp(“xxx”,“g”);到RegExp(/ xxx /,“g”);

You can do

你可以做

var data = /demo purpose\?/; // **OR**  var data = "demo purpose";
var sentence = "can I put these app as demo purpose?";
var re = new RegExp(data, "g");
console.log(sentence.match(re));   

and you will get the same output

你会得到相同的输出

#1


10  

you need to escape ? (i.e. write \\?) or else it would be interpreted as a quantifier in regex.

你需要逃脱吗? (即写\\?)或者它将被解释为正则表达式中的量词。

Furthermore, the \\b is not really necessary because it tries to match a non blank char in which case there is nothing behind demo purpose? so sentence.match(new RegExp("\\b(demo purpose\\?)\\b", "g")) would return null.

此外,\\ b并不是真的有必要,因为它试图匹配一个非空白字符,在这种情况下,没有什么可用于演示目的?所以sentence.match(新的RegExp(“\\ b(演示目的\\?)\\ b”,“g”))将返回null。

If you want randomness, use Math.random. Make an array and get an random integer or 0 or 1 (with Math.floor) as the index.

如果您想要随机性,请使用Math.random。创建一个数组并获得一个随机整数或0或1(使​​用Math.floor)作为索引。

#2


2  

In order to pass variables into JS regex when using constructor notation, you need to escape all characters that act as regex special characters (quantifiers, group delimiters, etc.).

为了在使用构造函数表示法时将变量传递给JS正则表达式,您需要转义所有充当正则表达式特殊字符的字符(量词,组分隔符等)。

The escape function is available at MDN Web site:

MDN网站上提供了转义功能:

function escapeRegExp(string){
  return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

Note also, that \b is a word boundary, and it prevents from matching the strings you need as ? is a non-word character. If you do not need to match word boundaries, remove \b. If you need to check if the search word is a whole word, use (?:^|\W) at the beginning and (?!\w) at the end and use exec rather than match to obtain access to the captured group 1.

另请注意,\ b是单词边界,它会阻止匹配您需要的字符串?是一个非单词字符。如果您不需要匹配单词边界,请删除\ b。如果你需要检查搜索词是否是整个单词,请在开头使用(?:^ | \ W),在结尾使用(?!\ w)并使用exec而不是匹配来获取对捕获的组1的访问权限。

So, your code will become:

那么,您的代码将变为:

function escapeRegExp(string){
  return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

var data = "demo purpose?";
var sentence = "can I put these app as demo purpose?";
var re = new RegExp("(?:^|\\W)(" + escapeRegExp(data) + ")(?!\\w)", "g");
while ((m = re.exec(sentence)) !== null) {
   console.log(m[1]);   // output ["demo purpose?"]
}

If you search for emo purpose?, no result will be returned since there will be no match.

如果您搜索emo目的?,将不会返回任何结果,因为没有匹配项。

#3


0  

This

var data = "demo purpose?"; // **OR**  var data = "demo purpose";
var sentence = "can I put these app as demo purpose?";
var re = new RegExp(/demo purpose\?/, "g");
console.log(sentence.match(re));   // output ["demo purpose?"]

return ["demo purpose?"], changing RegExp("xxx", "g"); to RegExp(/xxx/, "g");

return [“demo purpose?”],改变RegExp(“xxx”,“g”);到RegExp(/ xxx /,“g”);

You can do

你可以做

var data = /demo purpose\?/; // **OR**  var data = "demo purpose";
var sentence = "can I put these app as demo purpose?";
var re = new RegExp(data, "g");
console.log(sentence.match(re));   

and you will get the same output

你会得到相同的输出