Language: JavaScript
is required to match hello
, but cannot match abchello
. How should I write this regular rule? Have no idea ...
認(rèn)證0級(jí)講師
console.log(/\bhello\b/.test('abchello')); // false
console.log(/\bhello\b/.test('hello')); // true
/^hello$/, you may want qui to match the word hello, but not other words containing hello. In this case, you only need one hello, or write it with abc and return false. You can also write two
js does not have lookahead, it can only be determined through two matches.
/^hello$/ig.test('abchello'); //false
/hello$/ig.test('abchello'); //true
Cause:
^這個(gè)特殊字符表示以什么開始,如果想要隨意匹配就將其去掉。
同樣地,$這個(gè)特殊字符是結(jié)束標(biāo)志。
Personally, I think it’s better to use /bhellob/
, but this regular rule cannot be matched very accurately. It can only satisfy most situations, not special situations. This is because js’s support for regular expressions is not perfect enough, so you can make do with it for now