First off I don't know much about regex and need to buy a book because it's has proven to me to be difficult to pickup.
Ultimately I want to take a dom element, and replace text within straight brackets "[" and "]" and insert a link around the text, and there may be multiple bracket sets in the string.
function changeTip() {
var link = '<a href="' + $('#txtURL').attr('value') + '" target="_blank">';
$('.tipoftheweektip').html($('#txtTip').attr("value").replace('[', link).replace(']', '</a>'));
}
This works except:
I've looked at examples and because straight brackets are used in the regex code, I cant figure out how to look for a bracket and replace it.
Anyone out there that has done something similar that they can share? Thanks in advance.
.replace(/\[([^\]]*)\]/g, link + "$1</a>")
which means, find text between [ and ] and replace it with the value of link, the text itself and ''. This ensures matching square brackets. The 'g' means 'do it multiple times (globally)'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With