JavaScript regex: find non-numeric character

Let's say I have these two lines: "5/15/1983" and "1983.05.15". Suppose all characters in a string are numeric, with the exception of the delimiter character, which can appear anywhere in the string. There will be only one separator character; all instances of any 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];

Thank!

+5
source share
3 answers
"05-15-1983".match(/\D/)

, , , .

+13

, , .

:

<script>
var myStr1 = "1981-01-05";
var myStr2 = "1981-01-05";
var RegEx1 = /[0-9]/g;
var RegEx2 = /[^0-9]/g;
var RegEx3 = /[^0-9]/;
document.write( 'First : ' + myStr1.match( RegEx1 ) + '<br />' );
document.write( 'tooo : ' + myStr2.replace( RegEx2,  "" ) + '<br />' );
document.write( 'Second : ' + myStr1.match( RegEx2 ) + '<br />'  );
document.write( 'Third : ' + myStr1.match( RegEx3 ) + '<br />'  );
</script>

:

First : 1,9,8,1,0,1,0,5
tooo : 19810105
Second : -,-
Third : -

,

+1

Apparently tired or did not pay attention to my previous answer. Sorry about that. I had to write:

var regexp = new RegExp("([^0-9])","g");
var separator = regexp.exec("1985-10-20")[1];

Of course, Matthew Flashen works just as well. I just wanted to fix mine.

0
source

All Articles