search

Home  >  Q&A  >  body text

javascript - How to use regular expressions in js to find the string between two specific characters in a string?

For example, I now have a string "@3131 @aba21bas @zxcc@213wd cccaf21212"
I now want to extract all the characters between @ and spaces in this string
The above string has been extracted The final result should be 3131, aba21bas, zxcc@213wd
How should this regular expression be written? ? (js writing method)

曾经蜡笔没有小新曾经蜡笔没有小新2767 days ago654

reply all(2)I'll reply

  • 某草草

    某草草2017-06-07 09:25:17

    The match method in js cannot get the grouping results in the regular/g mode. For example, the @ and spaces cannot be removed from such results:

    "@3131 @aba21bas @zxcc@213wd cccaf21212".match(/@([^\s]*?)\s/g)
    //  ["@3131 ", "@aba21bas ", "@zxcc@213wd "]

    So you can change it to the reg.exec method, for example:

    function pick(str){
        var reg = /@([^\s]*?)\s/g, ret = [];
        while(arr = reg.exec(str)) ret.push(arr[1]);
        return ret;
    }
    pick("@3131 @aba21bas @zxcc@213wd cccaf21212" )

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-06-07 09:25:17

    reply
    0
  • Cancelreply