admin管理员组文章数量:1410697
I am attempting to search a string for a specific word ('cow') using the following:
var regex = new RegExp('cow', '\\b');
I only wish to target 'cow' and not words which contain 'cow' such as 'cowboy' or 'cows' using the '\b' expression, however this results in:
Uncaught SyntaxError: Invalid flags supplied to RegExp constructor '\b'
I have attempted to use 'b', '\b', '/\b' but all result in the same error.
What is the correct expression I need to use?
I am attempting to search a string for a specific word ('cow') using the following:
var regex = new RegExp('cow', '\\b');
I only wish to target 'cow' and not words which contain 'cow' such as 'cowboy' or 'cows' using the '\b' expression, however this results in:
Uncaught SyntaxError: Invalid flags supplied to RegExp constructor '\b'
I have attempted to use 'b', '\b', '/\b' but all result in the same error.
What is the correct expression I need to use?
Share Improve this question asked May 20, 2015 at 21:03 user1444027user1444027 5,2619 gold badges30 silver badges40 bronze badges2 Answers
Reset to default 2You're confusing the regular expression special characters with the flags, it should be:
var regex = new RegExp('\\bcow\\b', 'g');
The g
is the global flag, to search the supplied string for all matches.
References:
RegExp
.
2nd parameter for RegExp
object is flag like g
or i
etc. Just use
var regex = new RegExp('\\bcow\\b');
or simply use regex delimiter:
var regex = /\bcow\b/;
本文标签: javascriptUncaught SyntaxError Invalid flags supplied to RegExp constructor 39b39Stack Overflow
版权声明:本文标题:javascript - Uncaught SyntaxError: Invalid flags supplied to RegExp constructor 'b' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744318603a2600384.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论