I need to get the javascript replace() function to replace every instance of a [b] or [/b] with <b> or </b>. I tried this with the global tag but it doesn’t work and comes out oddly:
document.write(str.replace(/[b]/g,'<b>').replace(/[/b]/g,'</b>'));
results in the output being:
[<>]<>la<>la[<>][<>]<>la<>la[<>]
I also tried changing the first part to:
document.write(str.replace(/[[b]]/g,'<b>').replace(/[[/b]]/g,'</b>'));
which kind of works, but there’s odd ]s everywhere… like so:
[blabla[/[blabla[/
I know there’s probably something obvious I’m missing here… But I can’t find it… any help and I will love you forever XD
[,]and/are special RegExp characters.[a]means: Match anyacharacter.[abcd]means: Match one character, which is one of a, b, c, d.[a-z]means: Match any letter./is used to mark the beginning and end of a RegExp.To use the literal characters, they have to be escaped.
Note that both Regular expressions can be merged, using a backreference: