convert String to array in javascript "datastatusMonthly[0]" - javascript

datastatusMonthly[0] - This is my String in javascript
If i print this, it is printing as same string.
How do i get the value of '0' index in array datastatusMonthly using this above string?
Any help please?

You can use eval. The eval function will evaluate your string. JS bin here https://jsbin.com/guqoqukoqa/edit?js,console

Solution without eval, which is evil, using regex with group:
var datastatusMonthly = [3];
var text = 'datastatusMonthly[0]';
var regex = /(datastatusMonthly)\[([0-9]+)\]/g;
var match = regex.exec(text);
var arrayName = match[1];
var arrayIndex = match[2];
console.log(window[arrayName][arrayIndex]);

This dose't have to be in a String i guess. correct me if i am not understanding it properly
var fistElement = datastatusMonthly[0];
This link might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Accessing_array_elements

Related

Need a regular expression to split string in javascript

I need some help with regular expressions in javascript. I've got a string like:
var S = '["abc","defg", "hij"]';
How could I split it in javascript to get a[0]=abc, a[1]=defg, a[2]=hij?
Because var a = S.split(','); just give me a[0]=["abc" and so on.
Thank you very much.
If you fix the quotes you use to delimit the string, then you can JSON.parse the string to an array and work with it as you need, like this:
var s = '["abc","defg", "hij"]';
var arr = JSON.parse(s);
console.log(arr);
console.log(arr[0]); // = 'abc'
If you mean:
var S = ["abc", "def", "ghi"];
Then use S[0], S[1] and S[2].
If you mean:
var S = "[\"abc\", \"def\", \"ghi\"]";
Then use JSON parser
Complete example:
var S = "[\"abc\", \"def\", \"ghi\"]";
var SParsed = JSON.parse(S);
alert(SParsed [1]); //def

Need a Regex to get select name

I have a variable which stores some string.
eg var string = '<select mltiple="" name="multi_select_frV6Yzed4dxzsotOvJ5cXg9Aa[]" aria-required="true">';
i want to get multi_select_frV6Yzed4dxzsotOvJ5cXg9Aa[] using regex expression. Thanks in advance
You can do it this way as well (not using any regex here)
var string = '<select mltiple="" name="multi_select_frV6Yzed4dxzsotOvJ5cXg9Aa[]" aria-required="true">';
console.log($($.parseHTML(string)).attr("name")); //gives 'multi_select_frV6Yzed4dxzsotOvJ5cXg9Aa[]'
You can use this regex to retrieve that value:
var matches = /name="(.*)"\s/gi.exec(string);
console.log(matches[1]); // = "multi_select_frV6Yzed4dxzsotOvJ5cXg9Aa[]"
You would obviously need to make the code more robust to deal with cases where the attribute is not found.
You could also use jQuery:
console.log($(string).attr('name'));
See this working regex.
Here is the javascript code.
var re = /name="([^"]*)/g;
var str = '<select mltiple="" name="multi_select_frV6Yzed4dxzsotOvJ5cXg9Aa[]" aria-required="true">';
var m;
m = re.exec(str);
alert(m[1]);

how to get id from string which ends with certain word (regexp)

i want to get id which is ends with "_theTable" in string using regex. but i am not getting that . i am using this code:-
var str="<table id='dnn_ctl_123_theTable'><tr><td></td></tr></table>";
var rexexp = new RegExp("\b\w*_theTable\b");
var matchedwrd=rexexp.exec(str);
Please guide how to do this?
Thanks in Advance
When you use a new Regexp you have to escape your backslashes like so:
var rexexp = new RegExp("\\b\\w*_theTable\\b");
Or you can use a regex literal:
var rexexp = /\b\w*_theTable\b/;
var str="<table id='dnn_ctl_123_theTable'><tr><td></td></tr></table>"
var rexexp = /id='(.+?)_theTable'/;
var matchedwrd=rexexp.exec(str);
alert(matchedwrd[1]);
var str="<table id='dnn_ctl_123_theTable'><tr id='another'><td></td></tr></table>";
var regEx = /id='(.*?_theTable)'/;
var id = str.match(regEx)[1];
document.write(id);
​
I'd probably use the pattern id='([^']*)_theTable' for this. Then $1 should correspond to the portion of the id before _theTable. If you want to include the _theTable bit, just move the closing parenthesis after it.

get particular string part in javascript

I have a javascript string like "firstHalf_0_0_0" or secondHalf_0_0_0". Now I want to get the string before the string "Half" from above both strings using javascript.Please help me.
Thanks.
var myString = "firstHalf_0_0_0";
var parts = myString.split("Half");
var thePart = parts[0];
var str = 'firstHalf_0_0_0',
part = str.match(/(\w+)Half/)[1];
alert(part); // Alerts "first"
var str = "firstHalf.....";
var index = str.indexOf("Half");
var substring = str.substr(0, index);
jsFiddle demo.
Using this you can get any particular part of string.
var str= 'your string';
var result = str.split('_')[0];
Working example here for your particular case.
http://jsfiddle.net/7kypu/3/
cheers!

Finding REGEX for this Expression(javascript)

I have the following string in java script
href="http://site.com/colours/254359457969591" title="hello"
I need to get the value 254359457969591 from the above href string.I tried with many methods. Can anybody guide me to solve this problem?
Well, just /\d+/ would work in this example.
var s = "href=\"http://site.com/colours/254359457969591\" title=\"hello\"";
var result = /href="http:\/\/site.com\/colours\/(\d+)"/.exec(s);
var num = result[1];
The result of num is: 254359457969591
Here's how you would get it with javascript's Match function:
str = 'href="http://site.com/colours/254359457969591" title="hello"';
patt1 = /\d+/;
document.write(str.match(patt1));
UPDATE
If you want to get the numbers between colours/ and " then use this regex:
/colours\/(\d+)"/
Here's the match function updated:
str = 'href="http://site.com/colours/254359457969591" title="hello"';
patt1 = /colours\/(\d+)"/;
document.write(str.match(patt1)[1]);
m/href="http:\/\/site\.com/colours/([0-9]+)/i
$1 is the number.
Just use the javascript provided regex functions and that's it.

Categories

Resources