简体   繁体   中英

Regex: Get string in between characters or character and end of line

I've got these possible strings:

&modelIds=340&makeIds=4307,6548&stuffId=340
OR
&modelIds=340&makeIds=4307,6548

I'm trying to extract either an array of the makeIds or more simple the string of the makeIds (in this case "4307,6548") and I'll just split them by comma later on.

I've got it to work in the first case but not the second using:

var stringTest = "&modelIds=340&makeIds=4307,6548&stuffId=340"
var matches = stringTest.match(/makeIds=(.*)&\b/)

This returns: ["makeIds=4307,2342&", "4307,2342"] so I can do:

var ids = matches[1];

Unfortunately this does not work with the 2nd string. I've tried /makeIds=(.*)?(&|$)\\b/ but that doesn't work either.

You have to nest in the ? quantifier. It's an optional match. You meant to make your .* lazy:

/static_keywords=(.*?)(&|$)\b/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM