将此php正则表达式转换为javascript

时间:2012-03-04 11:26:32

标签: php javascript regex converter

我想知道如何将这个精确的php正则表达式功能更改为javascript?

$ismatch= preg_match('|She is a <span><b>(.*)</b></span>|si', $sentence, $matchresult);

if($ismatch)
{
      $gender= $matchresult[1];
}
else{ //do other thing }

1 个答案:

答案 0 :(得分:3)

这不是一件容易的事,因为JavaScript不支持s修饰符。

等效的正则表达式对象是

/She is a <span><b>([\s\S]*)<\/b><\/span>/i

代码的功能(如果匹配则从匹配中提取组1)将在JavaScript中完成,如下所示:

var myregexp = /She is a <span><b>([\s\S]*)<\/b><\/span>/i;
var match = myregexp.exec(subject);
if (match != null) {
    result = match[1];
} else {
    // do other thing
}