Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Total regular expressions virgin - string help!

Reply
Thread Tools

Total regular expressions virgin - string help!

 
 
Tony
Guest
Posts: n/a
 
      07-30-2005
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.

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      07-30-2005
Tony wrote:
> 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, '') );

">




--
Rob
 
Reply With Quote
 
 
 
 
cymrio@yahoo.com
Guest
Posts: n/a
 
      07-30-2005
> What RobG wrote:



Excellent, thanks Rob.

 
Reply With Quote
 
cymrio@yahoo.com
Guest
Posts: n/a
 
      07-30-2005
> What RobG wrote:



Excellent, thanks Rob.

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      07-30-2005
wrote:
>>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' );

--
Rob
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BT Total Broadband vs BT Business Total John Y UK VOIP 2 02-02-2007 09:27 AM
excluding search string in regular expressions Franz Steinhaeusler Python 9 10-22-2004 08:54 AM
Regular expressions as string delimiter? chad Java 4 03-06-2004 01:02 AM
Regular Expressions and String Replacement a_the_s@hotpop.com C++ 1 12-15-2003 07:31 AM
Add custom regular expressions to the validation list of available expressions Jay Douglas ASP .Net 0 08-15-2003 10:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57