简体   繁体   中英

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. The resulting string C should have no instances of A in it and zero white space.

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.

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. If it's not a match, pushing it to 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.

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. Any advice would be super helpful, I feel like I've hit a wall. Here's a link to my code in jsfiddle

Thanks, everyone.

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:

 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.

Another way, using recursion:

https://jsfiddle.net/iamnotsam/ytvujc9a/ (<-- is commented)

 // 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM