[英]Regular Expressions - Javascript - Single Quotes
Lets assume we have a couple variables. 假设我们有几个变量。 A. Str, a string,
John's
.... B. regex expression as shown below. A. Str,字符串,
John's
.... B.正则表达式如下所示。
inner.Html
DOES have John's
in it. inner.Html
确实有John's
内容。
What I dont get, is this code for regex never works when I have a str with a single quote in it. 我没有得到的是,当我有一个带有单引号的str时,此正则表达式代码永远无法正常工作。 Any ideas?
有任何想法吗?
var str = "John's"
var regex = new RegExp("\>[^\<]+?(" + str +")[^\>]+?\<", "gi");
var thismatch = regex.exec(this.innerHTML);
I am not sure about what your outer element looks like, but one main thing missing and the reason why your regex will not work when str has a single quote is because you need to escape the quote in the string. 我不确定您的外部元素是什么样子,但是缺少主要的一件事,以及当str带有单引号时您的正则表达式无法工作的原因是因为您需要对字符串中的引号进行转义。
var str = "John's".replace(/(\')/g, '\\$1'); // "John\'s";
var regex = new RegExp("\>[^\<]+?(" + str +")[^\>]+?\<", "gi");
var thismatch = regex.exec(this.innerHTML);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.