Easy A
Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  Latest imagesLatest images  SearchSearch  RegisterRegister  Log inLog in  

Share
 

 regex check please.

View previous topic View next topic Go down 
AuthorMessage
Vikram
Member
Member
Vikram

Posts : 6
Join date : 2011-04-18

regex check please. Empty
PostSubject: regex check please.   regex check please. EmptyFri Jun 17, 2011 2:46 am

hi,

Getting on quite well, I feel. just would like you to confirm that this regex will strip out characters that cannot be in a phone number

those are (imv) 0-9 - ( ) +
Code:

val = val.replace (/[^0-9\\s\\-\\(\\)\\+]/gi,"");  // strip the characters that cannot appear in a phone number

I use double backslashes to keep the perl interpreter happy.

otherwise (in case it simplifies it for you), it would be

Code:

val = val.replace (/[^0-9\s\-\(\)\+]/gi,"");  // strip the characters that cannot appear in a phone number
Back to top Go down
Zodiac
Member
Member
Zodiac

Posts : 2
Join date : 2011-05-05

regex check please. Empty
PostSubject: Re: regex check please.   regex check please. EmptyFri Jun 17, 2011 2:46 am

You don't need to escape most symbolic characters in a reg exp alternatives group([...]);

var phonenum= string.replace(/[^0-9 ()+-]+/g, '');

Being in a character group, the range of 0-9 and each literal character is a match for the expression.

You would need to escape the short-cut characters, \s, \w, \d and so on
(and of course any entities that require the slash in a string (\r,\t,\n, etc)).

var phonenum= string.replace(/[^\d ()+-]+/g, '');[/B]


You do need to escape slashes, backslashes, and a right hand square bracket ']'.

An unescaped hyphen must be the first or last character in the group-
otherwise it will try to make a range of its neighbors.

On a side note, I like to replace any non-digits in a phone number that are not a '+' in the first position, with spaces.

var string= '+370 (555)-555-1000+303' ;
var phonenum= ' '+(string.match(/((^\+?)|(\d+))/g) || []).join(' ');
/* returned value: (String)
+ 370 555 555 1000 303
*/
Back to top Go down
Vikram
Member
Member
Vikram

Posts : 6
Join date : 2011-04-18

regex check please. Empty
PostSubject: Re: regex check please.   regex check please. EmptyFri Jun 17, 2011 2:47 am

thanks for that. I'll ponder it for a while.
Back to top Go down
Sponsored content




regex check please. Empty
PostSubject: Re: regex check please.   regex check please. Empty

Back to top Go down
 

regex check please.

View previous topic View next topic Back to top 
Page 1 of 1

 Similar topics

-
» regex: disallow some domain names, allow others
» Check out my Tumblr!
» how to check if window is visible?

Permissions in this forum:You cannot reply to topics in this forum
Easy A :: regex check please. Edit-trash Useless :: Trash-