鍍金池/ 問答/HTML/ 關(guān)于正則去掉字符串后面的數(shù)字

關(guān)于正則去掉字符串后面的數(shù)字

比如
6月7號(hào)666
怎么去掉666

又比如
1號(hào)房間777
怎么去掉777

有沒有js正則。。。想了很久沒想到
找了很多方法都是匹配數(shù)字的,沒找到匹配字符串尾部數(shù)字的

回答
編輯回答
單眼皮

/\w\d+$/

2017年6月15日 00:45
編輯回答
祉小皓

抓住關(guān)鍵就可以了。末尾=>$,數(shù)字=>d。
拆分關(guān)鍵字照著正則表查元字符,拼一個(gè)正則。雖然這樣可能得出的不是最優(yōu)解,但是不失為一種解決問題的方法。

'6月7號(hào)666'.replace(/\d+$/,''); //"6月7號(hào)"
'1號(hào)房間777'.replace(/\d+$/,''); //"1號(hào)房間"
'6月7號(hào)999r'.replace(/\d+$/,''); //"6月7號(hào)999r"
2017年3月15日 19:05
編輯回答
刮刮樂
var reg = /\w\d+$/;
var str = '1號(hào)房間777';
console.log(str.match(reg)); //獲取匹配字符串
console.log(str.replace(reg,'')); //替換匹配字符串
2017年7月6日 10:00
編輯回答
乞許
var string= '1號(hào)3333房間777';
var matchStr= string.match(/[\u4e00-\u9fa5]\d+$/)[0];
return string.substr(0, string.indexOf(matchStr)+1)
2018年7月13日 12:29