penislandbic Posted January 5, 2014 Share Posted January 5, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/285099-replace-instances-from-array-in-string/ Share on other sites More sharing options...
requinix Posted January 5, 2014 Share Posted January 5, 2014 Loop over the array and do use String.replace for each item. Quote Link to comment https://forums.phpfreaks.com/topic/285099-replace-instances-from-array-in-string/#findComment-1463896 Share on other sites More sharing options...
Solution dungpt29 Posted January 5, 2014 Solution Share Posted January 5, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285099-replace-instances-from-array-in-string/#findComment-1463916 Share on other sites More sharing options...
Irate Posted January 5, 2014 Share Posted January 5, 2014 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). Quote Link to comment https://forums.phpfreaks.com/topic/285099-replace-instances-from-array-in-string/#findComment-1463933 Share on other sites More sharing options...
.josh Posted January 7, 2014 Share Posted January 7, 2014 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'); Quote Link to comment https://forums.phpfreaks.com/topic/285099-replace-instances-from-array-in-string/#findComment-1464319 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.