javascript - Search on string and capture value of specific HTML attribute -
i need regex src value of following strings, considering single, double , no quotes.
with double quotes:
var = '<script class="anyclass" src="anyfile.js" id="anyid">'; var b = '<script class="anyclass" src="anyfile.js">'; var c = '<script src="anyfile.js" id="anyid">'; var d = '<script src="anyfile.js">';
with single quotes:
var e = "<script class='anyclass' src='anyfile.js' id='anyid'>"; var f = "<script class='anyclass' src='anyfile.js'>"; var g = "<script src='anyfile.js' id='anyid'>"; var h = "<script src='anyfile.js'>";
without quotes:
var i= "<script class=anyclass src=anyfile.js id=anyid>"; var j= "<script class=anyclass src=anyfile.js>"; var k= "<script src=anyfile.js id=anyid>"; var l= "<script src=anyfile.js>";
expected match/return:
anyfile.js
i have poor looking solution split:
var file = ""; var match = 'src="'; if(a.indexof(match)>=0){ file = a.split(match); file = file[1]; file = file.split('.js"'); file = file[0] + ".js"; } else{ match = "src='"; if(a.indexof(match)>=0){ file = a.split(match); file = file[1]; file = file.split(".js'"); file = file[0] + ".js"; } else{ match = "src="; if(a.indexof(match)>=0){ file = a.split(match); file = file[1]; file = file.split('.js'); file = file[0] + ".js"; } else { file = "no match"; } } }
but can match wrong src
attributes, need regex.
any appreciated.
solution (special rajesh)
var = "<script class='anyclass' src='anyfile.js' id='anyid'>"; var b = '<script class="anyclass" src="anyfile.js">'; var c = '<script src="anyfile.js" id="anyid">'; var d = '<script src="anyfile.js">'; var e = "<script class='anyclass' src='anyfile.js' id='anyid'>"; var f = "<script class='anyclass' src='anyfile.js'>"; var g = "<script src='anyfile.js' id='anyid'>"; var h = "<script src='anyfile.js'>"; var i= "<script class=anyclass src=anyfile.js id=anyid>"; var j= "<script class=anyclass src=anyfile.js>"; var k= "<script src=anyfile.js id=anyid>"; var l= "<script src=anyfile.js>"; var l= "<script src=anyfile.js>"; function getsrc(str){ var regex = /src=["']*[^"' >]+/; var match = str.match(regex); if(match!==null){ match = match[0]; var replaceregex = /src=["' ]*/; match = match.replace(replaceregex, "") } else{ match = "no match"; } console.log(match); } [a,b,c,d,e,f,g,h,i,j,k,l].foreach(getsrc)
you can try this:
logic:
- search pattern
src=
followed single/double quotes [optional] followed thats not single/double quotes. - this yield
src="anyfile.js
. - now replace initials i.e.
src=
followed optional quote.
regex version
var = '<script class="anyclass" src="anyfile.js" id="anyid">'; var b = '<script class="anyclass" src="anyfile.js">'; var c = '<script src="anyfile.js" id="anyid">'; var d = '<script src="anyfile.js">'; var i= "<script class=anyclass src=anyfile.js id=anyid>"; var j= "<script class=anyclass src=anyfile.js>"; var k= "<script src=anyfile.js id=anyid>"; var l= "<script src=anyfile.js>"; function getsrc(str){ var regex = /src=["']*[^"' >]+/; var match = str.match(regex)[0]; var replaceregex = /src=["' ]*/; match = match.replace(replaceregex, "") console.log(match) } [a,b,c,d,i,j,k,l].foreach(getsrc)
Comments
Post a Comment