Home  >  Q&A  >  body text

javascript - js writes a regular expression to extract data from text

I have a text like this:

<p>sdsadsad</p><p><img src="/Edite/net/upload/image/20170701/6363450829304727881702482.jpg" title="ewe.jpg" alt="ewe.jpg"/><img src="/Edite/net/upload/image/20170701/6363450829304727881702483.jpg" title="ewe.jpg" alt="ewe.jpg"/></p>

Use js to write regular rules to extract part of the src value of all img tags in , and then use /@/# to extract the multiple values ​​ ##Splicing into a new string

20170701/6363450829304727881702482.jpg/@/20170701/6363450829304727881702483.jpg
给我你的怀抱给我你的怀抱2662 days ago745

reply all(2)I'll reply

  • 仅有的幸福

    仅有的幸福2017-07-05 10:45:25

    var html = '<p>sdsadsad</p><p><img src="/Edite/net/upload/image/20170701/6363450829304727881702482.jpg" title="ewe.jpg" alt="ewe.jpg"/><img src="/Edite/net/upload/image/20170701/6363450829304727881702483.jpg" title="ewe.jpg" alt="ewe.jpg"/></p>';
    
    var txt = html
        .match(/src="([^"]*?)"/g)
        .map(m => m.replace(/^src="\/Edite\/net\/upload\/image\/([^"]*)"$/, ''))
        .join('/@/');
    
    console.log(txt);

    Effect:

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-07-05 10:45:25

    const html = `<p>sdsadsad</p><p><img src="/Edite/net/upload/image/20170701/6363450829304727881702482.jpg" title="ewe.jpg" alt="ewe.jpg"/><img src="/Edite/net/upload/image/20170701/6363450829304727881702483.jpg" title="ewe.jpg" alt="ewe.jpg"/></p>`;
    console.log(html.match(/src="([^"]*?)"/g).map(m => m.replace('src="/Edite/net/upload/image/', '').replace(/"/g, '')).join('/@/'))

    Online experience

    reply
    0
  • Cancelreply