I have the following syntax.
var name = [Name_is][234]
var number = find [234];
How can i find this number in javascript/jquery which is inside []
?
I have the following syntax.
var name = [Name_is][234]
var number = find [234];
How can i find this number in javascript/jquery which is inside []
?
Use a regular expression.
var string = "[Name_is][234]"
var matches = string.match(/\[(\d+)]/);
if (matches.length) {
var num = matches[1];
}
Check out a working fiddle: http://jsfiddle.net/rEJ5V/, and read more on the String.match() method.
if you want to extract the text between the [ ]
, you can do:
var name = "[Name_is][234]";
var check= "\{.*?\}";
if (name.search(check)==-1) { //if match failed
alert("nothing found between brackets");
} else {
var number = name.search(check);
alert(number);
}