My input string looks something like:
var someString = 'This is a nice little string with <a target="_" href="/carSale/12/..">link1</a>. But there is more that we want to do with this. Lets insert another <a target="_" href="/carSale/13/..">link2</a> ';
My end goal is to match every anchor element that has a"carSale" within its href attribute and replace it with the text insider the anchor.
for e.g
Replace <a target="_" href="/carSale/12/..">link1</a> with string link1
but it should not replace
<a target="_" href="/bikeSale/12/..">link3</a>
since the above href does not contain the string "carSale"
I have created a regular expression object for this. But it seems to be performing a greedy match.
var regEx = /(<a.*carSale.*>)(.*)(<\/a>)/;
var someArr = someString.match(regEx);
console.log(someArr[0]);
console.log(someArr[1]);
console.log(someArr[2]);
console.log(someArr[3]);
Appending the modifier 'g' at the end fo the regular expression gives bizare results.
Fiddle here : http://jsfiddle.net/jameshans/54X5b/