0

I want to remove(technically replacing) the words int, float, char, bool, main, void main, int main, cout, cin to "" (like removing it) if it is found on the string.

So if i have the ff:

str = "void main(){  couts<<"wrong"; cout<<"right";  }"

After replacing, it should be:

str = "(){  ();  couts<<"wrong"; <<"right"; }"

The words int, float, char, bool, main... etc are stored inside an array

kWord[0] = /int/
kWord[1] = /float/
kWord[2] = /char/
kWord[3] = /bool/
kWord[4] = /main/
kWord[5] = /void\s+main/
kWord[6] = /int\s+main/
kWord[7] = /cout/
kWord[8] = /cin/

This is related to my previous question How to remove a part of the string the fastest way but this time the words are in Array

3
  • 2
    Not sure what I'm missing here but it seems like you can loop through your array and do a .replace() on the string for each index in the array. Or just create a single RegExp with all the words. Commented Aug 28, 2013 at 19:23
  • 1
    If the array contained strings you could turn it into a single regexp with var re = new RegExp('\b(' + kWord.join('|') + '\b'));. But if they're already regexps, you can just loop through them. But they need \b at the ends, and the g modifiers. Commented Aug 28, 2013 at 19:26
  • help: kWord[9] = /!string!/ is not being replaced.. Commented Aug 28, 2013 at 22:17

2 Answers 2

1
for(int i=0; i<kWord.length; i++) {
  str = str.replace(kWord[i],"");
}

of course this will also convert mainframe to frame, integer to eger, etc. It also won't hit all occurences, just the first. If you need to avoid substrings like that you might need to put some more thought into it, and use a regex that checks for word boundaries

/\bfoo\b/g
Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't modify the original str and it also doesn't address the g flag issue.
It's invalid to say "You don't need regexs at all. You can just put the original strings" when some of the replacements clearly require them.
@naomik good point, didn't look closely enough at the word list. better?
0

You can do the same as in the answer to your other question, but building a regex object using kWord.join('|').

kWord[0] = 'int';
kWord[1] = 'float';
kWord[2] = 'char';
kWord[3] = 'bool';
kWord[4] = 'main';
kWord[5] = 'void\\s+main';
kWord[6] = 'int\\s+main';
kWord[7] = 'cout';
kWord[8] = 'cin';

var r = '\\b(' + kWord.join('|') + ')\\b';
var myRegex = new RegExp(r, 'g');
str = str.replace(myRegex, "");

7 Comments

@fireflieslive use the [el1, el2, el3, ...] array notation instead of kWord = []; wWord[0] = el1; .... Also, if you plan to perform the replace often, make sure not to recreate a new RegExp object all the time.
@fireflieslive, it all works fine, but there's really no reason to use push if the content of the array isin't dynamic.
@plax: it will be dynamic, i just presented the constant side Ü
@tap: i have a kWord[9] = '!string'.. why it doesnt want to be replaced?
I personally think this is an abominable cross of strings and regexen.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.