开发者

Javascript remove leading number label from string

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开发者_Python百科?

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

Thanks!


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜