![](/img/trans.png)
[英]How to strip non-digit characters from an id attribute with JavaScript
[英]Remove non-digit characters from random string except first occurrence of #
這個問題看起來微不足道 - 但事實並非如此。 我想使用正則表達式從沒有第一個#
字符的字符串中刪除所有非數字字符。 您可以使用以下代碼段(並在那里編輯magic
函數)進行測試:
function magic(str) { // example hardcoded implementation - remove it and use proper regexp return str.replace(/#1234a5678b910/,'#12345678910'); } // Test tests = { // keys is input string, value is valid result for that input "#1234a5678b910": "#12345678910", "12#34a5678b910": "12#345678910", "1234a56#78b910": "123456#78910", "1234a5678b91#0": "1234567891#0", "1234a5678b91#0": "1234567891#0", "98#765a4321#039c": "98#7654321039", "98a765#4321#039c": "98765#4321039", "98a765b4321###39": "987654321#39", } Object.keys(tests).map(k=> console.log(`${k} Test: ${(''+(magic(k)==tests[k])).padEnd(5,' ').toUpperCase()} ( result is ${magic(k)} - should be ${tests[k]})`) );
輸入字符串以隨機方式生成。 到目前為止我嘗試過這個,但沒有運氣
function magic(str) {
return str.replace(/(?<=#.*)[^0-9]/g, '') ;
}
使用 replace 和 regexp 很熱嗎?
可變長度后視僅在某些 JavaScript 引擎 (EMCA2018) 中有效。 請在此處查看瀏覽器兼容性以了解后視斷言。
對於支持lookbehinds的引擎,您可以使用以下正則表達式:
(?<!^[^#]*(?=#))\D+
工作原理如下:
(?<!^[^#]*(?=#))
否定后視確保以下不匹配
^
斷言字符串開頭的位置[^#]*
匹配除#
以外的任何字符任意次數(?=#)
正向前瞻確保接下來是#
\\D+
匹配任何非數字字符一次或多次簡單來說, ^[^#]*(?=#)
匹配到遇到第一個#
的位置。 然后我們否定這些結果(因為我們不想替換每個字符串中的第一個#
)。 最后,我們匹配與這些位置不匹配的非數字字符\\D+
。
function magic(str) { // example hardcoded implementation - remove it and use proper regexp return str.replace(/(?<!^[^#]*(?=#))\\D+/g,''); } // Test tests = { // keys is input string, value is valid result for that input "#1234a5678b910": "#12345678910", "12#34a5678b910": "12#345678910", "1234a56#78b910": "123456#78910", "1234a5678b91#0": "1234567891#0", "1234a5678b91#0": "1234567891#0", "98#765a4321#039c": "98#7654321039", "98a765#4321#039c": "98765#4321039", "98a765b4321###39": "987654321#39", } Object.keys(tests).map(k=> console.log(`${k} Test: ${(''+(magic(k)==tests[k])).padEnd(5,' ').toUpperCase()} ( result is ${magic(k)} - should be ${tests[k]})`) );
此方法最適合跨瀏覽器支持(舊瀏覽器或當前不支持 EMCA2018 的瀏覽器)。
這使用兩個正則表達式來清理兩個子字符串:
[^\d#]+ # replace all characters that aren't digits or # (first substring)
\D+ # replace all non-digit characters (second substring)
function magic(str) { // example hardcoded implementation - remove it and use proper regexp i = str.indexOf('#') || 0 x = str.substr(0,i+1) y = str.substr(i+1) r = x.replace(/[^\\d#]+/g,'')+y.replace(/\\D+/g,'') //console.log([i,x,y,r]) return r } // Test tests = { // keys is input string, value is valid result for that input "#1234a5678b910": "#12345678910", "12#34a5678b910": "12#345678910", "1234a56#78b910": "123456#78910", "1234a5678b91#0": "1234567891#0", "1234a5678b91#0": "1234567891#0", "98#765a4321#039c": "98#7654321039", "98a765#4321#039c": "98765#4321039", "98a765b4321###39": "987654321#39", } Object.keys(tests).map(k=> console.log(`${k} Test: ${(''+(magic(k)==tests[k])).padEnd(5,' ').toUpperCase()} ( result is ${magic(k)} - should be ${tests[k]})`) );
很簡單 - 匹配包括第一個#
(如果有)的部分,然后替換第二組中的非數字。 之后,只需將它們粘在一起即可。
function magic(str) { let rx = /^([^#\\n]*\\#)(.*)/; let string = str.replace(rx, function(m, g1, g2) { if (g1.endsWith("#")) { part1 = g1.replace(/\\D+/g, "") + "#"; } else { part1 = g1.replace(/\\D+/g, ""); } return part1 + g2.replace(/\\D+/g, ""); }); return string; } // Test tests = { // keys is input string, value is valid result for that input "#1234a5678b910": "#12345678910", "12#34a5678b910": "12#345678910", "1234a56#78b910": "123456#78910", "1234a5678b91#0": "1234567891#0", "1234a5678b91#0": "1234567891#0", "98#765a4321#039c": "98#7654321039", "98a765#4321#039c": "98765#4321039", "98a765b4321###39": "987654321#39", } Object.keys(tests).map(k=> console.log(`${k} Test: ${(''+(magic(k)==tests[k])).padEnd(5,' ').toUpperCase()} ( result is ${magic(k)} - should be ${tests[k]})`) );
使用此正則表達式替換字符串([a-zA-Z])|(?<=#(.*?))#
。 這匹配az
中的所有字符以及AZ
和#
中的所有字符,后跟另一個 # 和字母。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.