Ok i have this problem... I want to take out some text from a string and that text is held between () brackets and the string looks like this var a = validate(password) I want to take out the password ;) thx in forward Vanja
+3
A:
Use plain old JS .replace()
method with an regular expression:
var a = "validate(password)";
a = a.replace(/\(.*?\)/g, "()");
// will result in a = "validate()"
This will remove every substring that is enclosed in brackets.
Update:
If you want to get the value, you can use .match()
:
var a = "validate(password)";
var match = a.match(/\(.*?(?=\))/);
This will get any value enclosed in brackets. But as JS does not support lookbehind, you will get "(password"
. Removing the first character is easy though:
match = match.substring(1);
Update 2
If you know that there will only be one value enclosed in brackets than you can also do it without regex:
a = a.substring(a.indexOf('(')+1, a.indexOf(')'));
Felix Kling
2010-04-09 12:45:40
I didn't want to remove password i wanted to take password out of that string and to put it in a new string...
vanjadjurdjevic
2010-04-09 13:34:48
+1 for the answer. `@vanjadjurdjevic` - you need to modify the question and phrase it properly. `filter` and `take out` sound very much like you want to remove the text from the string. for the sake of others that will not read this comment and try to answer the question, please update it.
Anurag
2010-04-09 13:40:24
A:
Ok this is the solution var str = "validate(password)"; var bar = str.substring(9); var end = bar.replace(')','');
vanjadjurdjevic
2010-04-09 13:35:43
Well this is very specific and would not work anymore if you have `"valid(password)"`.
Felix Kling
2010-04-09 15:36:16