Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.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中的Lookaround替代方案_Javascript_Html_Regex_Regex Lookarounds - Fatal编程技术网

JavaScript中的Lookaround替代方案

JavaScript中的Lookaround替代方案,javascript,html,regex,regex-lookarounds,Javascript,Html,Regex,Regex Lookarounds,我曾尝试在JavaScript中使用lookahead/lookbehind(如中所述),但发现它们不受支持 实际上,我想做的是捕捉一个dar,后面不是win。因此,字符串darblabla应该返回true,而darwinblabla应该是false 现在,我的解决方案是:/dar/i.test(string)&&/达尔文/i.test(字符串) 这看起来很长,是否有任何更短的解决方案(regex字符串?)可以替代当前语句?Javascript应该支持负前瞻 警报(/dar(?!win)/.t

我曾尝试在JavaScript中使用lookahead/lookbehind(如中所述),但发现它们不受支持

实际上,我想做的是捕捉一个
dar
,后面不是
win
。因此,字符串
darblabla
应该返回
true
,而
darwinblabla
应该是
false

现在,我的解决方案是:
/dar/i.test(string)&&/达尔文/i.test(字符串)


这看起来很长,是否有任何更短的解决方案(regex字符串?)可以替代当前语句?

Javascript应该支持负前瞻

警报(/dar(?!win)/.test('darblahblah'))
警报(/dar(?!win)/.test('darwin'))
这里有一个解决方案

^dar(?!win)

编辑

关于你的第二个问题

^(?!cyg)win


在JavaScript中不能使用look behinds,但可以使用look aheads

因此,这将产生
false


document.write(/\bdar(?!win)/i.test(“达尔文”)语法错误:无效正则表达式:/dar(?win)/:无效组您忘记添加
@Shaels正如Debuggex中所解释的,您只需要检查单词是否以
dar
开头,后面是否紧跟
win
。Debuggex是开发regex的好工具,谢谢!是否可以转换
/win/i.test(字符串)&/cyg win/i.test(string)
以同样的方式?@Shaels是的,您需要反转测试。检查我的更新注意
(?!cyg)
^(?!cyg)win
中的
(?!cyg)
检查前3个字符是否不是
cyg
,因为前3个字符必须是
win
!这样做的目的是投票赞成或反对答案,并接受最好的答案。