kingshuk basak kingshuk basak - 1 year ago 78
Javascript Question

Regex match for a specific pattern excluding a specific pattern

I have sample string as :

  1. '&label=20:01:27&tooltext=abc\&|\|cba&value=6|59|58|89&color=ff0000|00ffff'

  2. '&label=20:01:27&tooltext=abc\&|\|cba&value=6|59|58|89'

My objective is to select the text from '
' till the first occurrence of '
' which is not preceded by
. I'm using the following regex :


and the

It is working fine for the second string but for the first string it is selecting upto the last occurrence of '
' not preceded by

var a = '&label=20:01:27&tooltext=abc\&|\|cba&value=6|59|58|89&color=ff0000|00ffff',
b = a.match(/(tooltext=)(.*)([^\\])(&)(.*[^\\]&)?/i)


b = ["tooltext=abc\&|\|cba&value=40|84|40|62&", "tooltext=", "abc\&|\|cba&value=40|84|40|6", "2", "&"]

But what i need is:

b = ["tooltext=abc&||cba&", "tooltext=", "abc&||cb", "a", "&"]

Answer Source

I think you can use a regex like this:


[Regex Demo]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download