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.
例如-1:我有像“aaa-23-34”这样的字符串,我想要这个字符串是“aaa-23”
例如-2:var str = "aaa-44-34-12"
var str = "aaa-44-34-12"
输出应该是"aaa-44-34"
"aaa-44-34"
这意味着我想要最后一个连字符之前的字符串。
基本字符串操作:
> "aaa-44-34-12".split('-').slice(0, -1).join('-') "aaa-44-34"
尝试一个简单的正则表达式
"aaa-23-34".replace(/-\d+$/, '');
前任:
var str = "aaa-44-34-12"; var txt = str.replace(/-\d+$/, '');