Javascript regex to extract all characters between quotation marks following a specific word

后端 未结 2 858
情深已故
情深已故 2021-01-22 00:34

I have the text:

s.events=\"event3\"
s.pageName=\"Forum: Index\"
s.channel=\"forum\"
s.prop1=\"Forum: Index\"
s.prop2=\"Index Page\"
s.prop36=\"\"
s.prop37=\"\"
         


        
2条回答
  •  无人及你
    2021-01-22 01:33

    Use this:

    var myregex = /s\.prop42="([^"]*)"/;
    var matchArray = myregex.exec(yourString);
    if (matchArray != null) {
        thematch = matchArray[1];
    } 
    

    In the regex demo, look at the capture group in the right pane.

    Explanation

    • s\.prop42=" matches s.prop42=" (but we won't retrieve it)
    • The parentheses in ([^"]*) capture any chars that are not a " to Group 1: this is what we want
    • The code gets the Group 1 capture

提交回复
热议问题