This question already has an answer here: How do you use a variable in a regular expression? 16 answers Use new RegExp(string) to build a regular expression dynamically. The literal /../ form cannot be used with dynamic content. Make sure to have a valid pattern after building the string. var len = 99; var re = new RegExp(".(?=.{" + len + "})", "g"); var output = input.replace(re, "*")
这个问题在这里已经有了答案: 你如何在正则表达式中使用变量? 16个答案 使用new RegExp(string)动态构建正则表达式。 文字/../表格不能用于动态内容。 确保在构建字符串后有一个有效的模式。 var len = 99; var re = new RegExp(".(?=.{" + len + "})", "g"); var output = input.replace(re, "*") 另见(并投票赞成): 你如何在正则表达式中使用变量?
This question already has an answer here: How do you use a variable in a regular expression? 16 answers The RegExp constructor takes a string and creates a regular expression out of it. function name(str,replaceWhat,replaceTo){ var re = new RegExp(replaceWhat, 'g'); return str.replace(re,replaceTo); } If replaceWhat might contain characters that are special in regular expressions,
这个问题在这里已经有了答案: 你如何在正则表达式中使用变量? 16个答案 RegExp构造函数接受一个字符串并从中创建一个正则表达式。 function name(str,replaceWhat,replaceTo){ var re = new RegExp(replaceWhat, 'g'); return str.replace(re,replaceTo); } 如果replaceWhat可能包含正则表达式中特殊的字符,则可以执行: function name(str,replaceWhat,replaceTo){ replaceWhat = replaceWhat.replace(/[
This question already has an answer here: How do you use a variable in a regular expression? 16 answers You have to use RegExp : str.match(new RegExp(pattern1+'|'+pattern2, 'gi')); When I'm concatenating strings, all slashes are gone. If you have a backslash in your pattern to escape a special regex character, (like ( ), you have to use two backslashes in the string (because is the
这个问题在这里已经有了答案: 你如何在正则表达式中使用变量? 16个答案 您必须使用RegExp : str.match(new RegExp(pattern1+'|'+pattern2, 'gi')); 当我连接字符串时,所有斜杠都消失了。 如果你的模式中有一个反斜杠来转义一个特殊的正则表达式字符(比如( ),你必须在字符串中使用两个反斜杠(因为是字符串中的转义字符): new RegExp('\(')将与/(/ 。 所以你的模式必须成为: var pattern1 = ':\(|:=
I've been trying to find a way to match a number in a Javascript string that is surrounded by parenthesis at the end of the string, then increment it. Say I have a string: var name = "Item Name (4)"; I need a RegExp to match the (4) part, and then I need to increment the 4 then put it back into the string. This is the regex I have so far: b([0-9]+)$b This regex does not work. Further
我一直在试图找到一种方法来匹配字符串末尾的圆括号所包围的Javascript字符串中的数字,然后对其进行增量。 说我有一个字符串: var name = "Item Name (4)"; 我需要一个RegExp来匹配(4)部分,然后我需要增加4,然后把它放回到字符串中。 这是我迄今为止的正则表达式: b([0-9]+)$b 这个正则表达式不起作用。 此外,我不知道如何提取检索到的整数并将其放回到字符串中的相同位置。 谢谢。 替换方法可以将函数作为
So for example: function(input){ var testVar = input; string = ... string.replace(/ReGeX + testVar + ReGeX/, "replacement") } But this is of course not working :) Is there any way to do this? var regex = new RegExp("ReGeX" + testVar + "ReGeX"); ... string.replace(regex, "replacement"); Update Per some of the comments, it's important to note that you may want to escape the va
例如: function(input){ var testVar = input; string = ... string.replace(/ReGeX + testVar + ReGeX/, "replacement") } 但是,这当然不工作:)有没有办法做到这一点? var regex = new RegExp("ReGeX" + testVar + "ReGeX"); ... string.replace(regex, "replacement"); 更新 根据一些评论,重要的是要注意,如果存在恶意内容的潜在可能性(例如,变量来自用户输入),您可能想要转义该变量, 您可以使
Possible Duplicate: Is there a RegExp.escape function in Javascript? I am trying to build a javascript regex based on user input: function FindString(input) { var reg = new RegExp('' + input + ''); // [snip] perform search } But the regex will not work correctly when the user input contains a ? or * because they are interpreted as regex specials. In fact, if the user puts an unba
可能重复: Javascript中有RegExp.escape函数吗? 我正在尝试基于用户输入构建一个javascript正则表达式: function FindString(input) { var reg = new RegExp('' + input + ''); // [snip] perform search } 但是,当用户输入包含一个正则表达式时,正则表达式将无法正常工作? 或*因为它们被解释为正则表达式特殊项。 事实上,如果用户放置一个不平衡的(或[在他们的字符串中,正则表达式甚至不是有效的。
This question already has an answer here: How do you access the matched groups in a JavaScript regular expression? 14 answers 截至2018年,Javascript最终支持lookbehind断言,所以一旦它实现,以下应该在最新的浏览器中工作: test = "i am sam"; console.log(test.match(/(?<=i'm |i am )[a-zA-Z]+/)) You could capture your words using a capturing group ([a-zA-Z]+) : I ?['a]m ([a-zA-Z]+) Thi
这个问题在这里已经有了答案: 如何访问JavaScript正则表达式中的匹配组? 14个答案 截至2018年,Javascript最终支持lookbehind断言,所以一旦它实现,以下应该在最新的浏览器中工作: test = "i am sam"; console.log(test.match(/(?<=i'm |i am )[a-zA-Z]+/)) 你可以使用捕获组([a-zA-Z]+)捕捉你的单词: I ?['a]m ([a-zA-Z]+) 这会匹配 I # Match I ? # Match an optional white space
This question already has an answer here: How do you access the matched groups in a JavaScript regular expression? 14 answers 试试吧"#foo#bar".match(/[a-zA-Z]+/gi); //outputs ["foo", "bar"]
这个问题在这里已经有了答案: 如何访问JavaScript正则表达式中的匹配组? 14个答案 试试吧"#foo#bar".match(/[a-zA-Z]+/gi); //outputs ["foo", "bar"]
This question already has an answer here: What is the difference between RegExp's exec() function and String's match() function? 4 answers How do you access the matched groups in a JavaScript regular expression? 14 answers Yes, a capture group does this. You don't see it in your array because you've used String#match and the g flag. Either remove the g flag (and take the
这个问题在这里已经有了答案: RegExp的exec()函数和String的match()函数有什么区别? 4个答案 如何访问JavaScript正则表达式中的匹配组? 14个答案 是的,一个捕获组执行此操作。 你没有在数组中看到它,因为你已经使用了String#match和g标志。 删除g标志(并从数组中获取第二个条目,这是第一个捕获组): console.log('https://twitter.com/pixelhenk/status/891303699204714496'.match(//status/(d+)/)[1]);
This question already has an answer here: How do you access the matched groups in a JavaScript regular expression? 14 answers 你需要使用捕获正则表达式组()来分别提取数字和字符串,看看: let rawStr = "this is (131PS) for now"; let theMatch = rawStr.match(/((d+)([A-Z]+))/); if (theMatch) { let theNum = parseInt(theMatch[1]); let theString = theMatch[2]; console.log(theNum, the
这个问题在这里已经有了答案: 如何访问JavaScript正则表达式中的匹配组? 14个答案 你需要使用捕获正则表达式组()来分别提取数字和字符串,看看: let rawStr = "this is (131PS) for now"; let theMatch = rawStr.match(/((d+)([A-Z]+))/); if (theMatch) { let theNum = parseInt(theMatch[1]); let theString = theMatch[2]; console.log(theNum, theString); }