Jump to content

Replace instances from array in string


penislandbic
Go to solution Solved by dungpt29,

Recommended Posts

I want to replace key words from an array in a user input string for all instances and case insensitive with the string "NOPE". An example array would be...

var replacer = new Array("@", "$", "*", "drip", "slide", "move", "(", "\)");

Example:

var userInput = "I have to slide down @ $3.00 with all (*) of the $ and moVe to another driping SLIDe";

*Magic replacement...*

var output = "I have to NOPE down NOPE NOPE3.00 with all NOPENOPENOPE of the NOPE and NOPE to another NOPEing NOPE";

How would I do that?

 

 

 

Link to comment
Share on other sites

  • Solution

requinix's hint is good but obviously it is not enough to solve your problem. That is because JavaScript is a case-sensitive programming language and the output string must be in its original format after processing and the replacement must be case-insensitive. This is also the most difficult obstacles needed to overcome.

 

Here, I put forward some directions that can actualize an effective solution:

  • Convert the input string to uppercase (or lowercase).
  • Convert the keywords in the array to uppercase (or lowercase).
  • Specify the position of the first occurrence of the converted keyword in the converted input string.
  • Based on the located position, split the original string into two substrings (called s1 and s2) with the keyword used as separator. Clearly, it is not simple to use String.split to achieve this.
  • Create a new input string using concat method: the input string = concat(s1, "NOPE", s2);

The pseudo-code to describe the solution should be as the following:

Loop through the keyword array
    while (possible to locate the position of the keyword)
       Split the input string into two substrings s1 and s2 using keyword as separator;
       the new input string = concat(s1, "NOPE", s2);
    end while
End Loop

The result string = the final input string;

If you cannot write the effective code, I will be ready to help you further.

Link to comment
Share on other sites

Or just use regular expressions if you don't want to convert to lower-/upper-case and then back to the original state.

 

You can't dynamically create regular expression literals, so you have to use the /* new */ RegExp() constructor (the new is actually optional as it will work the same either way).

Link to comment
Share on other sites

uh yeah, you can use regex for this easy. Only thing you really have to do is double escape symbols that mean something to the regex engine:

 

var replacer = new Array("@", "\\$", "\\*", "drip", "slide", "move", "\\(", "\\)");
replacer = replacer.join('|');
var userInput = "I have to slide down @ $3.00 with all (*) of the $ and moVe to another driping SLIDe";
userInput=userInput.replace(RegExp(replacer,'ig'),'NOPE');
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.