Regex to extract substring, returning 2 results for some reason

后端 未结 5 1373
情话喂你
情话喂你 2020-11-28 08:13

I need to do a lot of regex things in javascript but am having some issues with the syntax and I can\'t seem to find a definitive resource on this.. for some reason when I d

5条回答
  •  抹茶落季
    2020-11-28 08:23

    Just get rid of the parenthesis and that will give you an array with one element and:

    • Change this line

    var test = tesst.match(/a(.*)j/);

    • To this

    var test = tesst.match(/a.*j/);

    If you add parenthesis the match() function will find two match for you one for whole expression and one for the expression inside the parenthesis

    • Also according to developer.mozilla.org docs :

    If you only want the first match found, you might want to use RegExp.exec() instead.

    You can use the below code:

    RegExp(/a.*j/).exec("afskfsd33j")

提交回复
热议问题