Home  >  Q&A  >  body text

javascript - How to write this regular rule to match a string that does not start with a certain character?

Language: JavaScript

is required to match hello, but cannot match abchello. How should I write this regular rule? Have no idea ...

PHP中文网PHP中文网2686 days ago743

reply all(6)I'll reply

  • 高洛峰

    高洛峰2017-06-12 09:23:00

    console.log(/\bhello\b/.test('abchello')); // false
    console.log(/\bhello\b/.test('hello')); // true

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-12 09:23:00

    reply
    0
  • 漂亮男人

    漂亮男人2017-06-12 09:23:00

    /^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

    reply
    0
  • PHP中文网

    PHP中文网2017-06-12 09:23:00

    js does not have lookahead, it can only be determined through two matches.

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-12 09:23:00

    /^hello$/ig.test('abchello'); //false
    /hello$/ig.test('abchello'); //true

    Cause:

    ^这个特殊字符表示以什么开始,如果想要随意匹配就将其去掉。
     同样地,$这个特殊字符是结束标志。

    reply
    0
  • 世界只因有你

    世界只因有你2017-06-12 09:23:00

    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

    reply
    0
  • Cancelreply