skip to Main Content
var str = '<img src="test>test" alt="test > test test" > "some text';
var reg = ???

How to make '<img src="test>test" alt="test > test test" >">'.match(reg)[0] === '<img src="test>test" alt="test > test test" >'

Tried this regexp /<s*(img)[^>]+(>[r|n|s]*</1>|/?>)/, but not worked.

2

Answers


  1. var str = '<img src="test>test" alt="test > test test" > "some text';
    var reg = /<img[^>]*>/;
    var result = str.match(reg)[0];
    console.log(result);
    
    
    
    Output: '<img src="test>test" alt="test > test test" >'
    
    Login or Signup to reply.
  2. <img – starting a tag
    [^">]* – any number before the first "
    (?:"[^"]+"[^">]*)* – any number of " pairs with no " inside the pair plus any number of chars except " and > after the pair
    > – closing

    const tests = ['<img src="test>test" alt="test > test test" > "some text', '<img> <img>', `<img
    src="test">`];
    
    tests.forEach(str => console.log(str.match(/<img[^">]*(?:"[^"]+"[^">]*)*>/g)));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search