[英]Need advice on how to compare, and remove occurrences of, one string from another in javascript?
I've got a problem that's been driving me kind of nuts. 我遇到了一个困扰我的问题。 I have to write a function that takes two strings, stringA and stringB, and using stringA remove all instances of stringA from stringB.
我必须编写一个接受两个字符串stringA和stringB的函数,并使用stringA从stringB中删除stringA的所有实例。 The resulting string C should have no instances of A in it and zero white space.
结果字符串C中应该没有A的实例,并且空格应为零。
Test cases would be: 测试用例为:
stringA = 'ab';
stringB = 'aabb';
stringC = ' ';
or 要么
stringA = ')(';
stringB = '((()))'
stringC = stringB;
So far my general approach has been to start with two prompts stored in variables that collect the strings. 到目前为止,我一般的方法是从存储在收集字符串的变量中的两个提示开始。 .replace() is run on both in order to remove any white space.
.replace()都在两者上运行以删除任何空白。
The closest I've gotten was two for loops, one for stringA with string B nested inside, and testing the indices against eachother for matches. 我得到的最接近的是两个for循环,一个是stringA,其中嵌套了字符串B,并针对彼此测试索引是否匹配。 If it's not a match, pushing it to stringC.
如果不匹配,则将其推入stringC。 But my problem is, its not really removing the instances of stringA only if the two indices in question happen to match at that time.
但是我的问题是,仅当有问题的两个索引当时恰好匹配时,它才不会真正删除stringA实例。
I've searched around here quite a bit and came up with .filter() or .replace() but I'm not having any luck/insight on how exactly to use those. 我已经在这里搜索了很多,并提出了.filter()或.replace(),但是我对如何使用它们没有任何运气/见识。 Any advice would be super helpful, I feel like I've hit a wall.
任何建议都将非常有帮助,我觉得我碰壁了。 Here's a link to my code in jsfiddle
这是我在jsfiddle中的代码的链接
Thanks, everyone. 感谢大家。
UPDATE UPDATE
You guys are amazing. 你们很棒。 :) I don't want to just steal an answer and pass it in, but you've all given me a wealth of things to look into and understand so that when I DO solve it, I understand the why and not just the what.
:)我不想只是偷偷地回答一个问题并将其传递给我,但是你们所有人都给了我很多东西供我研究和理解,以便在我解决它时,我理解为什么而不是什么。 Thanks so much for the fast answers, help, and guidance.
非常感谢您的快速解答,帮助和指导。
You can do like this : 您可以这样:
var stringA = 'ab', stringB = 'aabb'; do { var before = stringB; stringB = stringB.replace(stringA, ''); } while (stringB != before); alert(stringB);
You need to perform global search and replace plus one additional replace for remaining string. 您需要执行全局搜索并替换,并对剩余的字符串再进行一次替换。
var stringA = 'ab';
var stringB = 'aabb';
var stringC;
var regex = new RegExp(stringA, "g");
stringC = stringB.replace(regex, "").replace(regex, "");
console.log(stringC);
If you need to remove all occurrences of a Word A inside Word B until no match is found , in loops, you can use: 如果需要循环删除词B中所有出现的词A, 直到找不到匹配项 ,可以循环使用:
function escapeRegExp(string){ return string.replace(/[.*+?^${}()|[\\]\\\\]/g, "\\\\$&"); } var stringRemover = function() { var stringA = "()"; var stringB = 'a((()))b'; var stringC = stringB; var rx = RegExp(escapeRegExp(stringA), "g"); while (stringC.indexOf(stringA) > -1) { stringC = stringC.replace(rx, ""); } document.getElementById("t").innerHTML = stringC; } stringRemover();
<script src="app.js"></script> <title>challange</title> <body> <h1 id="t">String Remover</h1> </body>
It is important to use a regex-based replace to remove all occurrences (with g
modifier it is global) and check in a loop if Word B still contains Word A. To make a correct dynamic regex, we should use an escapeRegExp
function. 重要的是使用基于正则表达式的替换来删除所有出现的内容(使用
g
修饰符是全局的),并在循环中检查Word B是否仍然包含escapeRegExp
。要制作正确的动态正则表达式,我们应该使用escapeRegExp
函数。
Another way, using recursion: 另一种方法,使用递归:
https://jsfiddle.net/iamnotsam/ytvujc9a/ (<-- is commented) https://jsfiddle.net/iamnotsam/ytvujc9a/(<-被注释)
// removes all instances of a string s1 from another string s2 var removeAllSubstrings = function(s1, s2, s3) { if (s2 === '' || s2 === s3) { return s2; } return removeAllSubstrings(s1, s2.replace(s1,''), s2); } var sa = 'ab', sb = 'doaaabbbaabbababne'; // Put the final string on the screen document.getElementById("t").innerHTML = removeAllSubstrings(sa, sb);
<script src="app.js"></script> <title>challange</title> <body> <h1 id="t">String Remover</h1> </body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.