I’d like to do a replace on a string that contains sq- or sq. I was thinking of doing something like this:
var imgSrc = event.dataTransfer.getData('Text');
imgSrc = imgSrc.replace('sq-', 'mt-') || imgSrc.replace('sq.', 'mt.');
Any ideas on how I can get this working?
You could do it with one regex…
jsFiddle.
The character class will match a literal
.(because it loses its special meaning in a character class) and a literal-(doesn’t look like a range so it has no special meaning either).The match is placed in capturing group
1, which is referenced in the replacement with$1.Performance test between one regex and two.