sorry, being dumb morning, don't know regular expressions, have created want use https://regex101.com/
but... can't use code suggest in javascript without escaping first.
here's regex: (?<=color:\s)([a-z]+)
which, want (matching word after color: in css file)
but, code suggest use in js is:
var re = /(?<=color:\s)([a-z]+)/g; var str = ' color: black'; var m; while ((m = re.exec(str)) !== null) { if (m.index === re.lastindex) { re.lastindex++; } // view result using m-variable. // eg m[0] etc. }
the first line, won't work, escaped to: var re = /\(?<=color:\s\)([a-z]+)/i
stops javascript error, won't match strings more.
what doing wrong?
as aside... can point me expanding regex exclude followed bracket? trying color names only, "color: black;" should match, "box-shadow: black... etc" should match, ideally, not "color: rgb(... etc"
it true js not support look-behinds, although there workarounds:
- reverse string , matches enables using look-aheads
- use capturing groups
- use while loop re.lastindex manipulation
in case, easier use capturing group:
var re = /\bcolor:\s*([a-z]+)/ig; var str = ' color: black'; var m; while ((m = re.exec(str)) !== null) { if (m.index === re.lastindex) { re.lastindex++; } // m[1] holding our value! document.getelementbyid("res").innerhtml = m[1]; }
<div id="res"/>
Comments
Post a Comment