Javascript re-expression to get src url between script tag
I wanted to get the script name from a line like this:
var text = '<script src="scripts/044c7c5e.vendor.js"></script><script src="scripts/fa9f85fb.scripts.js"></script>'
I wanted to get the middle name script ie fa9f85fb.scripts . How can I achieve this with javascript regex?
I am writing something like this:
text.match(new RegExp(/<script src="scripts\/[(.*?)]\.scripts\.js"><\/script>/), 'g')[0]
But its returning the entire string.
source to share
Your drawing grabbing doesn't work a little; [(.*?)]
should be (.*?)
simple:
/<script src="scripts\/(.*?)\.scripts\.js"><\/script>/g
will be a full regex, no need to call the class constructor RegExp
. The corresponding string is stored in the index 0
. The various segments are 1
then stored from the index onwards.
text.match( /<script src="scripts\/(.*?)\.scripts\.js"><\/script>/g )[1]
source to share
Try it /\w+.scripts(?=.js)/
?
Link: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
Your matching pattern is a bit vague. I can just use / fa9f85fb.scripts / to match it.
source to share