最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Javascript remove leading number label from string - Stack Overflow

programmeradmin3浏览0评论

I have some strings that look like this:

1.     Some stuff
2.     some more stuff
...
26.    Even more stuff

What is a good way to remove the leading number labels on these strings in javascript?

(Each line is a separate string in a separate variable)

Thanks!

I have some strings that look like this:

1.     Some stuff
2.     some more stuff
...
26.    Even more stuff

What is a good way to remove the leading number labels on these strings in javascript?

(Each line is a separate string in a separate variable)

Thanks!

Share Improve this question asked May 4, 2011 at 4:27 Chris DutrowChris Dutrow 50.4k67 gold badges195 silver badges262 bronze badges 1
  • str.replace(/\d+\./g,'') are each line in a seperate string? – Ibu Commented May 4, 2011 at 4:32
Add a comment  | 

6 Answers 6

Reset to default 8
str = str.replace(/^\d+\.\s*/, '');
var s = "26.    Even more stuff";

s.replace(/^[0-9]+/g, '')
"123. some text".match(/^[0-9]+\.\s+(.*)$/)[1] // "some text"

You can use a regular expression that matches the beginning of the string, any number of digits, the period and the white space after it:

s = s.replace(/^\d+\.\s+/, '');

This will leave the string unchanged if it would happen to look differently.

Just this line:

s = "26.    Even more stuff";
s = s.replace(/^[^.]+\./, "")

OUTPUT

    Even more stuff

str = "2. some more stuff";

str = str.replace(/[0-9]+.\s+/, "");

发布评论

评论列表(0)

  1. 暂无评论