-1

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 [] ?

Jimmy
  • 41
  • 3

2 Answers2

0

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.

Matt
  • 74,352
  • 26
  • 153
  • 180
0

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);
}
JMax
  • 26,109
  • 12
  • 69
  • 88