I am using jquery .attr in an input type so when I run this
console.log('name: '+$(this).attr('name'));
output is: name: user_project_category[65][skill][9]
How can I get the 65 and 9?
I am using jquery .attr in an input type so when I run this
console.log('name: '+$(this).attr('name'));
output is: name: user_project_category[65][skill][9]
How can I get the 65 and 9?
You can use Regular Expressions to extract text from between the brackets into an array and then you can access the array to get the values you want, you can either extract all text between brackets or just the numbers:
var yourInput = "user_project_category[65][skill][9]";
var allMatches = yourInput.match(/\[(.*?)\]/g);
console.log(allMatches[0]);
console.log(allMatches[2]);
var numberMatches = yourInput.match(/\[([0-9]*?)\]/g);
console.log(numberMatches[0]);
console.log(numberMatches[1]);
var data = "name: user_project_category[65][skill][9]";
console.log(data.split("[")[1].slice(0,-1))//use split to get 65] use slice to remove ]
console.log(data.split("[")[3].slice(0,-1))//use split to get 9] use slice to remove ]
Assuming this is not dynamic and format is the same. for dynamic use regex
Use regex.
var output = 'user_project_category[65][skill][9]';
var numbers = output.match(/\d+/g).map(Number);
alert(numbers);
output: 65,9
Do whatever you want to do with number.
Use regular expression or split function in JavaScript
var output= 'user_project_category[65][skill][9]';
output.split(/(\d+)/)[1];
output.split(/(\d+)/)[3];