Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

javascript - 从随机字符串中删除非数字字符,但第一次出现的是#(Remove non-digit characters from random string except first occurrence of #)

This question looks trivial - but is not.(这个问题看起来微不足道-但事实并非如此。)

I want using regexp to remove all non-digits characters from string without first # character.(我想使用regexp从字符串中删除所有非数字字符,而没有第一个#字符。) You can use below snippet (and edit magic function there) to tests:(您可以使用下面的代码片段(并在其中编辑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]})`) ); The input string is generated in random way.(输入字符串以随机方式生成。) I try this so far but no luck(我到目前为止尝试了这个,但是没有运气) function magic(str) { return str.replace(/(?<=#.*)[^0-9]/g, '') ; } Hot to do it using replace and regexp ?(热门使用replace和regexp吗?)   ask by Kamil Kie?czewski translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Variable length lookbehinds only work in certain JavaScript engines (EMCA2018).(变长后视仅适用于某些JavaScript引擎(EMCA2018)。)

See browser compatibility for lookbehind assertions here .(请参阅浏览器兼容性,了解此处的后置断言。) Regex method(正则表达式方法) For the engines that do support lookbehinds, you can use the following regex:(对于确实支持后向搜索的引擎,可以使用以下正则表达式:) (?<!^[^#]*(?=#))D+ Works as follows:(工作原理如下:) (?<!^[^#]*(?=#)) negative lookbehind ensuring the following does not match((?<!^[^#]*(?=#))否定向后确保以下内容不匹配) ^ assert position at the start of the string(^在字符串开头的断言位置) [^#]* match any character except # any number of times([^#]*匹配除#以外的任何字符任意次数) (?=#) positive lookahead ensuring what follows is #((?=#)积极向前看,确保随后的是#) \D+ match any non-digit character one or more times(\D+匹配任何非数字字符一次或多次) In simpler terms, ^[^#]*(?=#) matches up to the position where the first # is encountered.(简单来说, ^[^#]*(?=#)匹配到遇到第一个#的位置。) We then negate these results (since we don't want to replace the first # in each string).(然后,我们对这些结果求反(因为我们不想替换每个字符串中的第一个# )。) And finally, we match the non-digit characters \D+ that don't match those positions.(最后,我们匹配与这些位置不匹配的非数字字符\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]})`) ); String manipulation method(字符串操作方法) This method works best for cross-browser support (older browsers or those that don't currently support EMCA2018).(此方法最适合跨浏览器支持(较旧的浏览器或当前不支持EMCA2018的浏览器)。) This uses two regular expressions to clean both substrings:(这使用两个正则表达式清除两个子字符串:) [^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]})`) );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...