Javascript Regexp - Match Characters after a certain phrase

后端 未结 5 446
名媛妹妹
名媛妹妹 2020-12-13 03:36

I was wondering how to use a regexp to match a phrase that comes after a certain match. Like:

var phrase = \"yesthisismyphrase=thisiswhatIwantmatched\";
var          


        
5条回答
  •  甜味超标
    2020-12-13 04:16

    You use capture groups (denoted by parenthesis).

    When you execute the regex via match or exec function, the return an array consisting of the substrings captured by capture groups. You can then access what got captured via that array. E.g.:

    var phrase = "yesthisismyphrase=thisiswhatIwantmatched"; 
    var myRegexp = /phrase=(.*)/;
    var match = myRegexp.exec(phrase);
    alert(match[1]);
    

    or

    var arr = phrase.match(/phrase=(.*)/);
    if (arr != null) { // Did it match?
        alert(arr[1]);
    }
    

提交回复
热议问题