Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.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中使用包含特殊字符的正则表达式_Javascript_Regex - Fatal编程技术网

如何在JavaScript中使用包含特殊字符的正则表达式

如何在JavaScript中使用包含特殊字符的正则表达式,javascript,regex,Javascript,Regex,抱歉,今天早上我可能很笨,但我对正则表达式了解不多,但我已经创建了一些我想使用的东西 但是。。。我不能在Javascript中使用他们建议的代码而不首先转义它 下面是正则表达式:(?JS确实不支持look behinds,尽管: 反转字符串,然后匹配允许使用look aheads的字符串 使用捕获组 使用带有re.lastIndex操作的while循环 在这种情况下,使用捕获组要容易得多: var re=/\b颜色:\s*([a-z]+)/ig; var str='颜色:黑色'; var-

抱歉,今天早上我可能很笨,但我对正则表达式了解不多,但我已经创建了一些我想使用的东西

但是。。。我不能在Javascript中使用他们建议的代码而不首先转义它


下面是正则表达式:
(?JS确实不支持look behinds,尽管:

  • 反转字符串,然后匹配允许使用look aheads的字符串
  • 使用捕获组
  • 使用带有re.lastIndex操作的while循环
在这种情况下,使用捕获组要容易得多:

var re=/\b颜色:\s*([a-z]+)/ig;
var str='颜色:黑色';
var-m;
while((m=re.exec(str))!==null){
如果(m.index==re.lastIndex){
re.lastIndex++;
}
//m[1]保持着我们的价值!
document.getElementById(“res”).innerHTML=m[1];
}

JS确实不支持look behinds,尽管:

  • 反转字符串,然后匹配允许使用look aheads的字符串
  • 使用捕获组
  • 使用带有re.lastIndex操作的while循环
在这种情况下,使用捕获组要容易得多:

var re=/\b颜色:\s*([a-z]+)/ig;
var str='颜色:黑色';
var-m;
while((m=re.exec(str))!==null){
如果(m.index==re.lastIndex){
re.lastIndex++;
}
//m[1]保持着我们的价值!
document.getElementById(“res”).innerHTML=m[1];
}

js不支持lookbehindUse:
/color:\s([a-z]+)/i
和抓取捕获组#1js不支持lookbehindUse:
/color:\s([a-z]+)/i
和抓取捕获组#1js不支持lookbehindUse:
/color:\s([a-z]+)/i
和抓取捕获组#
var re = /(?<=color:\s)([a-z]+)/g; 
var str = ' color: black';
var m;

while ((m = re.exec(str)) !== null) {
    if (m.index === re.lastIndex) {
        re.lastIndex++;
    }
    // View your result using the m-variable.
    // eg m[0] etc.
}