Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有这样的字符串:
xxx-字符串-字符串
其中xxx可以是 1 到 5 个字符长的数字。我需要选择它们和第一个-字符并将它们从字符串中删除。我该怎么做(我不擅长正则表达式,我慢慢开始失去理智:D)?
您可以使用:
var n = '12345-string-string'; var r = n.replace(/^\d{1,5}-/, ''); //=> string-string
说明: \d{1,5}-将匹配长度为 1 到 5 的数字,后跟一个连字符,^然后确保匹配字符串的开头。
\d{1,5}-
^