简体   繁体   English

Javascript正则表达式从单词的开头删除数字。

[英]Javascript Regex to remove numbers from the start of a word.

I'm trying to clean up text and it's formatted like this: 我正在尝试清理文本,它的格式如下:

1In the 1在

12The answer 12答案

6अनाथ 6अनाथ

Title 标题

5-16These are 5-16这些是

I want to remove any numbers and dashes anytime they are on the front of a word but am failing. 我想删除任何数字和短划线,只要它们在一个单词的前面,但我失败了。 This is close and works in the first two cases: 这很接近并适用于前两种情况:

str.replace(/^[^A-Z]*/,"")

but when the language changes like the third case it removes everything. 但是当语言像第三种情况一样改变时,它会删除所有内容。

The last case has a line break but this didn't work: 最后一个案例有一个换行符,但这不起作用:

.replace(/^[^\r\n|\r|\n^A-Z]*/,"")

Any ideas? 有任何想法吗?

If you only want replace decimal digits and hyphens then just include those in your character class: 如果您只想要替换十进制数字和连字符,那么只需在您的角色类中包含这些数字:

str.replace(/^[\d-]*/,"")

If you'd also like to remove all whitespace (including line breaks), try this: 如果您还想删除所有空格(包括换行符),请尝试以下操作:

str.replace(/^[\d\s-]*/,"")

I think this will do: 我想这会做:

str.replace(/^[-\d\s]*/,"")

Live code: http://regexr.com/38kkk 现场代码: http//regexr.com/38kkk

Explanation: 说明:

  • - is literal dash. -是文字破折号。 Note that if it is to be placed as a literal character inside [] , it should be placed right after [ . 请注意,如果要将其作为文字字符放在[] ,则应将其放在[
  • \\d stands for digits \\d代表数字
  • \\s stands for white space characters (spaces, tabs, new lines) \\s代表空白字符(空格,制表符,新行)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM