Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript regex: find non-numeric character

Let's say I have these two strings: "5/15/1983" and "1983.05.15". Assume that all characters in the string will be numeric, except a "separator" character that can appear anywhere in the string. There will be only one separator character; all instances of any given non-numeric character in the string will be identical.

How can I use regex to extract this character? Is there a more efficient way than the one below?

"05-15-1983".replace(/\d/g, "")[0];

Thanks!

like image 419
JamesBrownIsDead Avatar asked Aug 05 '10 01:08

JamesBrownIsDead


1 Answers

"05-15-1983".match(/\D/)

Technically, this returns an array containing one string, but it will implicitly convert to the string most places you need this.

like image 95
Matthew Flaschen Avatar answered Oct 01 '22 01:10

Matthew Flaschen