javascript - Regex matching to exclude beginning word -
how can make following javascript
'id=d41c14fb-1d42&'.match(/(?![id=])(.*)[^&]/)[0]
return "d41c14fb-1d42"? returns "41c14fb-1d42", without beginning "d".
this should it:
'id=d41c14fb-1d42&'.match(/^id=(.*)&$/)[1] // "d41c14fb-1d42"
~ edit
when original question not give context hard guess need exactly, in case give solution yields requested output. understand want able parse query string, i.e., name1=value1&name2=value2...
. following regular expression yields namex
followed optional =valuex
, believe valid provide parameter in query string without value.
var parameters = "id=foobar&empty&p1=javascript&p2=so".match(/\w+(=([^&]+))?/g) // ["id=foobar", "empty", "p1=javascript", "p2=so"]
you can split on "=" obtain parameter , value separately.
var param1 = parameters[0].split("=") // ["id", "foobar"]
for parameter without value yield array 1 value, of course.
"empty".split("=") // ["empty"]
note assumes query parameter match \w+
, if there other cases have expand regular expression.
if want match id
parameter anywhere in query string , obtain value, infer comment below, can use:
"id=donald".match(/(?:^|&)id=([^&]+)/)[1] // "donald" "param1=value1&id=donald¶m2=value2".match(/(?:^|&)id=([^&]+)/)[1] // "donald"
Comments
Post a Comment