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]);
Related
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
I have the following string, "blahblahhellothere", that I would like to be shortened to "hellothere" using JavaScript and/or JQuery.
I have tried using the following code:
var titletext123 = "blahblah<br>hellothere"
var injuryt3xt = titletext123.substring(titletext123.indexOf("<br>") +1);
Which only returns "br>hellothere".
Does anyone have any code which will get rid of the and all text before it?
Thank you very much. All of your help is appreciated!
Make it
var titletext123 = "blahblah<br>hellothere" var injuryt3xt = titletext123.substring(titletext123.indexOf("<br>") + 4);
So it is +4. Which accounts for all the characters in <br>.
You can use split() and get second element.
var titletext123 = "blahblah<br>hellothere" ;
var injuryt3xt = titletext123.split("<br>")[1];
alert(injuryt3xt);
Using regular expression:
var text = "blahblah<br>hellothere"
var clipped = str.replace(/.+\<br\>/, ""));
Another option (depending on circumstances) might be:
var injuryt3xt = titletext123.split("<br>")[1];
Which would split the string on <br> and return an array with the left-over parts ... the second of which is referred to with the [1]
After reading the post here, I tried to get the value by regex as below:
var myString = "<a href='/search.html?id=HDJ&area=ASD&estate=JKG&ppt=3'></a>";
var myRegexp = /&estate=(.*?)(?:\s|$)/g;
var match = myRegexp.exec(myString);
match[1]
The result was JKG&propertytype=3'></a>, but I only want JKG. Strictly speaking I want the value between &estate= and &ppt Could someone suggest how to do that?
Thanks
A Regular Expression:
/&estate=(.*?)&ppt=/g
Note: I wouldn't recommend using regular expressions to parse query strings. It's brittle. Consider if the variables in the query string change order. If that can be the case, I recommend reading - Parse query string in JavaScript.
do:
var myString = "<a href='/search.html?id=HDJ&area=ASD&estate=JKG&ppt=3'></a>";
var myRegexp = /estate=(.*)&ppt=/g;
var match = myRegexp.exec(myString);
console.log( match[1] );
If only the solution is important then you can use the following:
var myString = "<a href='/search.html?id=HDJ&area=ASD&estate=JKG&ppt=3'></a>";
var indx1 = mystring.indexOf("&estate=") + 8;
var indx2 = mystring.indexOf("&ppt");
var neededString = mystring.substring(indx1, indx2);
Just exclude ampersands from the selection:
var myRegexp = /&estate=([^&]+)/g;
You might want to change it to this, in case estate is the first parameter:
var myRegexp = /[\?&]estate=([^&]+)/g;
jsFiddle
Based on your result value, just split it at the ampersand, no new or alternate regex required.
JavaScript:
var myString = "<a href='/search.html?id=HDJ&area=ASD&estate=JKG&ppt=3'></a>";
var myRegexp = /&estate=(.*?)(?:\s|$)/g;
var match = myRegexp.exec(myString);
var value = match[1].split('&')[0];
alert( value );
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.
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!