Replace String with empty string
11,283
Hope this helps
var input = ' form + " form " + form " form + " " + form "';
var tmp = input.replace(new RegExp(/\"?\s*form\s*\"?\s*\+\s*\"?/g), ''); //form +
tmp = tmp.replace(new RegExp(/\"?\s*\+\s*form\s*\"?\s*\"?/g), '');// + form
alert(tmp);
Had to create two Regex expression one for case where + is before 'form' and another where it comes after. There can be better ways. Some notes below to help understand.
\s* -> zero or more spaces
\"? -> zero or one double quote
\+ -> one + symbol
/../g -> replace all matches
Comments
-
MohanRajNK over 1 year
I have a string that can have the following substring values (includes quotes too)
"+form" "+ form " " form+" "form +" + form +form form+ form + form + + form .... .... .... or just simply 'form' that doesnt surrounded by double quotes
The problem is to find the substring (+ form or form +) that matches the following
- '+ form' should not be surrounded by quotes
number of spaces between the '+' and 'form' are not limited
If it is found, then it should be replaced by the empty string ""
Input: ' form+ "form" + form ' Output: "form"
Input: ' form' Output: 'form'
Any help?
I'm just at beginner level, and it seems that I cannot solve this with simple replace and index of method :-(
var abc = string.replace(" " + ""); if(abc.indexOf("+form") > -1 || abc.indexOf("form+") > -1 || abc.indexOf("form") > -1 || abc.indexOf("\"+form\"") > -1 || || abc.indexOf("\"form+\"") > -1 ) { // then what should do? }
-
MohanRajNK over 9 yearsIt is not recursive and i need the original string with changes made.. Above code can only modifies the trimmed string
-
n32303 over 9 yearsif you input ' form+ "form" + form ' in the above code, you will get "form" for output.. Try the edit I made