Shoutbox

[RESOLVED] Need help with JavaScript replace() - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: [RESOLVED] Need help with JavaScript replace() (/showthread.php?tid=93685)

[RESOLVED] Need help with JavaScript replace() by macgyver08 on 01-28-2010 at 11:03 AM

I found a regular expression that will strip my string of every html tag...

code:
var stripped = htmlStr.replace(/(<([^>]+)>)/ig,"");

Well, I also want it to strip every thing between parentheses, square brackets, and replace "&#160;" with a space.

It would be really cool if you could explain how to do this as well, because the stuff in the above code after "replace" confuses me. I have no idea how that's working.
RE: Need help with JavaScript replace() by stoshrocket on 01-28-2010 at 12:18 PM

quote:
Originally posted by macgyver08
I found a regular expression that will strip my string of every html tag...

code:
var stripped = htmlStr.replace(/(<([^>]+)>)/ig,"");

Well, I also want it to strip every thing between parentheses, square brackets, and replace "&#160;" with a space.

It would be really cool if you could explain how to do this as well, because the stuff in the above code after "replace" confuses me. I have no idea how that's working.

the replace function is using Regular Expressions, in the format of /pattern/modifiers. In this example, everything between the "/"s are the characters to replace and the ig are the modifiers (in this case, i for case insensitive and g for global [replaces all instances, not just the first]).

So, you can modify everything between the /'s to strip whatever character you want.

As for replacing "&#160" with a space, just use another replace function...

javascript code:
var stripped = htmlStr.replace(/(<([^>]+)>)/ig,"");
stripped = stripped.replace(/&#160/g," ");

RE: Need help with JavaScript replace() by macgyver08 on 01-28-2010 at 01:18 PM

Thanks a lot! :D