Javascript get value in attribute - javascript

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 ]
You can use split with slice
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.
working fiddle

Use regular expression or split function in JavaScript
var output= 'user_project_category[65][skill][9]';
output.split(/(\d+)/)[1];
output.split(/(\d+)/)[3];

Related

How can I get words from a string, textarea javascript

I've been working on JavaScript and HTML, I have a text area where the user sets a CSV like this:
17845 hello bye 789
Now I have 17845,hello,bye,789 and I need to extract the values between the commas. I've tried with index Of, but what if the user sets 2 lines instead of 1, how can I get these words? I have thought of separate them getting the "\n".
Javascript function split() will do the trick
var str = '17845,hello,bye,789';
var words = str.split(',');
console.log(words);
Use javascript split() Function
Split function gives u the array.
var sentence ="hello, 123, tedsfd, demo";
var strArr = sentence.split(',');
$.each(strArr,function(key,value){
console.log(value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
If I understand it correctly, you are facing issues if the user sets comma separated value in multiple lines instead of one... For that use trim() function to remove all tabs and newline and then use split() function.
Use String.split.It converts strings into arrays, given you provide the delimiter, to separate the string.
var userInput = '17845, hello, bye, 789';
data = userInput.split(',');
console.log(data);
//data[0] = '17845'
//data[1] = 'hello'
//data[2] = 'bye'
//...

remove quotes before an array list using jquery

I have been strugling with a problem, I am using
var srlisthidden = $('#hiddenroutList').val();
srlisthidden returns an array of list but in quotes "['0015','0016']"
$.each(srlisthidden, function(i, value) {
});
But because of the double quotes on the beginning of the array,it is not allowing the list to iterate even, I tried many different options to remove the double quotes like regEx and
jQuery.parseJSON('['+srlisthidden+']'), but none of them worked, Please give me solution.
Try this out:
var x = "['0015','0016']"; // The value that you are grabbing
var sol = eval(x); // This returns the value as an array of strings.
Is this what you are trying to achieve?
You can achieve it like this as well
var to_parse = "['0015','0016']";
var array = parse.replace(/\[|]|'/g, '').split(',');
JSON requires inner quotes around strings be double-quotes escaped with a backslash; the parser doesn't play nicely with single quotes.
Clean up your string with regex:
var str = srlisthidden.replace(/\'/g, "\"")
Output: ["0015","0016"] (as a string)
Then parse as JSON:
JSON.parse(str)
Output: ["0015", "0016"] (as an array)

Covert a sequence of continuous characters into array in javascript

I have been trying days to convert a sequence of characters into array .Lets say my sequence would be like var text="abcde"..without any space in the string.All i Wanna do is i wanna convert text into an array like text=[a,b,c,d,e];
Is there any way to do it using javascript .Please Help me
Use split
var array = text.split("");
Use split():
var text = 'abcde',
textArray = text.split(''); // ['a','b','c','d','e']
JS Fiddle demo.
References:
String.split().

How can I parse a value out of a string with javascript?

I a string name content that has inside the text "data-RowKey=xxx". I am trying to get out xxx so I tried the following:
var val = content.substring(12 + content.indexOf("data-RowKey="), 3);
This does not work at all. rather than just get three characters I get a very long string. Can anyone see what I am doing wrong
You're using wrong tool. When you want to capture a data matching some pattern, you should use regular expressions. If your value is exactly three symbols, correct expression would be /data-RowKey=(...)/ with . standing for any symbol and () specifying part to capture.
.substring() [MDN] takes two indexes, .substr() [MDN] takes an index and the length. Try:
var val = content.substr(12 + content.indexOf("data-RowKey="), 3);
If "data-RowKey=xxx" is the whole string, there are various other ways to get xxx:
var val = content.replace('data-RowKey=', '');
var val = content.split('=')[1]; // assuming `=` does not appear in xxx
This works:
var value = content.match(/data-RowKey=(.*)/)[1];
Live DEMO
If there could be values after the xxx, use this:
"data-RowKey=123abc".match(/data-RowKey=(.{3}).*/)[1] // 123
If your rowkey is numeric, this might be best since you get the number as an integer and wouldn't need to convert later:
var val = parseInt( content.split("data-RowKey=")[1] );
If always the three characters and/or no need to convert:
var val = content.split("data-RowKey=")[1].substring(0,3);

Using regex to extract a double digit number from a string

I want to extract the "73" from this string:
wiring_details_1-6-73
The numbers 1, 6 and 73 are all vars so I need it to be value independant, I found this website which generated this regex:
.*?\\d+.*?\\d+.*?(\\d+)
But that seems a bit overkill and complicated, what is the cleanest method to extract the "73"?
var string = "wiring_details_1-6-73"
var chunks = string.split("-");
alert(chunks[chunks.length-1]) //73
demo: http://jsfiddle.net/eawBT/
You can use this /\d{1,2}$/
var str = "wiring_details_1-6-73";
var result = str.match(/\d{1,2}$/);
this returns an array with the matched number as string and you can access the value as result[0].

Categories

Resources