Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / July 2005



Tip: Looking for answers? Try searching our database.

Total regular expressions virgin - string help!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tony - 30 Jul 2005 05:30 GMT
Via regular expressions, how do I replace characters in a string with
""(nothing) if they aren't in an array of allowed characters?

var allowed = ["x","y"," "];

var string = "xyz w yz"; - replace everything not in allowed array.
RobG - 30 Jul 2005 06:43 GMT
> Via regular expressions, how do I replace characters in a string with
> ""(nothing) if they aren't in an array of allowed characters?
>
> var allowed = ["x","y"," "];
>
> var string = "xyz w yz"; - replace everything not in allowed array.

<input type="text" value="zxujh yjhg xzhgt" onblur="

 var allowed = [ 'x', 'y', ' '];
 var re = new RegExp( '[^' + allowed.join('') + ']', 'g' );

 alert( this.value.replace( re, '') );

">

Signature

Rob

cymrio@yahoo.com - 30 Jul 2005 13:18 GMT
> What RobG wrote:

Excellent, thanks Rob.
RobG - 31 Jul 2005 00:44 GMT
>>What RobG wrote:
>
> Excellent, thanks Rob.

The use of an array is not necessary, the allowed characters can be a
simple string:

 var allowed = 'xy ';
 var re = new RegExp( '[^' + allowed + ']', 'g' );

Signature

Rob

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.