JavaScript正则表达式:查找非数字字符 [英] JavaScript regex: find non-numeric character

查看:642
本文介绍了JavaScript正则表达式:查找非数字字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这两个字符串:5/15/1983和1983.05.15。假设字符串中的所有字符都是数字,除了可以出现在字符串中任何位置的分隔符字符。只有一个分隔符;字符串中任何给定的非数字字符的所有实例都是相同的。

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];

谢谢!

推荐答案

"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.

这篇关于JavaScript正则表达式:查找非数字字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆