Heim  >  Fragen und Antworten  >  Hauptteil

javascript - Wie schreibe ich diese reguläre Regel so, dass sie mit einer Zeichenfolge übereinstimmt, die nicht mit einem bestimmten Zeichen beginnt?

Sprache: JavaScript

Erfordert Übereinstimmung hello,但不能匹配 abchello, wie schreibe ich diesen regulären Ausdruck? Ich kann es mir nicht vorstellen...

PHP中文网PHP中文网2686 Tage vor742

Antworte allen(6)Ich werde antworten

  • 高洛峰

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

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

    Antwort
    0
  • 我想大声告诉你

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

    Antwort
    0
  • 漂亮男人

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

    /^hello$/, 你可能是要qui匹配hello这个单词,其他的含有hello的不要,这样的话,只要一个hello,或者写成有abc的返回false,写两个也可以

    Antwort
    0
  • PHP中文网

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

    js没有前瞻环视, 只能通过两次匹配来确定.

    Antwort
    0
  • 我想大声告诉你

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

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

    原因:

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

    Antwort
    0
  • 世界只因有你

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

    个人觉得还是用/\bhello\b/比较好,但是这个正则也无法很准确的匹配到,只能满足大多数情况,不能满足特殊的情况。这是因为js对正则支持不够完善的缘故,先可以这么将就着用

    Antwort
    0
  • StornierenAntwort