search

Home  >  Q&A  >  body text

regexp - How to "match but not contain" a certain condition with JavaScript's regular expression?

/images/img2.jpg" alt="" class="img"

As shown in the above code, there are two "img" words. I want to match the "img" in the first "img2", but does not include "2". I don't know how to achieve it.

If /img[0-9]/ is used, "2" is also included. Is there any way to match numbers followed by numbers, but the result does not include numbers?

过去多啦不再A梦过去多啦不再A梦2802 days ago936

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-06-12 09:30:12

    /(img)d/.exec('/images/img2.jpg" alt="" class="img"')[1]
    match img and have numbers after it, the specific number of numbers depends on your match, and finally take it out The data in the matching group is img, grouped by parentheses

    .

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-12 09:30:12

    \bimg+(?=\d)

    Thanks for the comment. It turns out that this is a suitable situation for using zero-width assertions. I looked up the information and wrote one.
    The plus sign is preceded by the result to be matched ("img"), followed by the selection condition (meaning that img must contain a number), this solves the problem~
    O(∩_∩)O~~

    reply
    0
  • Cancelreply