简体   繁体   中英

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?

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

Thanks!

str = str.replace(/^\d+\.\s*/, '');
"123. some text".match(/^[0-9]+\.\s+(.*)$/)[1] // "some text"
var s = "26.    Even more stuff";

s.replace(/^[0-9]+/g, '')

Just this line:

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

OUTPUT

    Even more stuff

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.

str = "2. some more stuff";

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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