Jump to content


Photo

String replace, caps insensitive... what's a better option?

javascript string replace

Best Answer requinix, 21 December 2013 - 04:21 AM

Use regular expressions:

orig = orig.replace(/&lt;br&gt;/gi, "<br>");
/g is what makes it replace every instance while /i gives you the case insensitivity. Go to the full post


  • Please log in to reply
1 reply to this topic

#1 LLLLLLL

LLLLLLL
  • Members
  • PipPipPip
  • Advanced Member
  • 308 posts

Posted 21 December 2013 - 04:03 AM

I just found code that does this:

			orig = orig.replace( "&lt;br&gt;", "<br>" );
			orig = orig.replace( "&lt;BR&gt;", "<br>" );
			orig = orig.replace( "&lt;bR&gt;", "<br>" );
			orig = orig.replace( "&lt;Br&gt;", "<br>" );

I see why it's done this way (to encompass any caps variation on the BR) but since .replace() will only get the first instance, this is just bad all the way around. Also, there's no way to do a toLowerCase() because that will mess up the rest of the string.

 

Is there some sort of JS regex way to replace all instances of &lt;br&gt; regardless of the caps? 

 

Thanks!



#2 requinix

requinix
  • Administrators
  • Lazy Administrator
  • 9,373 posts
  • LocationWA

Posted 21 December 2013 - 04:21 AM   Best Answer

Use regular expressions:
orig = orig.replace(/&lt;br&gt;/gi, "<br>");
/g is what makes it replace every instance while /i gives you the case insensitivity.

Edited by requinix, 21 December 2013 - 04:23 AM.

The Reimann Zeta Function Trolley Problem | "Summer is when I, the great ice fairy, can show my true power!"




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users