html - jQuery Regex not picking up -
ok, have checked 4 different questions , none have given me answer. :(
a website working on had few hundred pages unfortunately structure of tags in url has changed. , guess what, pages hard coded text , links (fml) anyway quick fix query change these links.
i decided try using this:
$(document).ready(function() { $('a').each(function() { var str2 = "tag="; var link = $(this).attr('href'); if(link.indexof(str2) != -1){ var link = link.replace("/tag=.*/","tag/$1"); $(this).attr('href', link); } }); })
what need value of 'href' scan 'tag=ecommerce' or 'tag=b2b' etc , change structure 'tag/eccommerce' or 'tag/b2b'
however not working.
what missing?
you need use regex syntax /yourregex/
instead of "/yourregex/"
. , use capturing group so:
$(document).ready(function() { $('a').each(function() { var str2 = "tag="; var link = $(this).attr('href'); if(link.indexof(str2) != -1){ var link = link.replace(/tag=(.*)/,"tag/$1"); $(this).attr('href', link); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <a href="www.foo.com/tag=mytag">mylink</a>
Comments
Post a Comment