Finding REGEX for this Expression(javascript) - 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.

Related

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

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

How to trim string between slash?

I need to take vale between / slashes. for example ./ankits/ankitt$. Here I need to take the ‘antits’ string. Do i need to use reg-expression or it can be done using trim method?
Please help me to solve this
What about .split method?
var str = './ankits/ankitt$.';
var arr = str.split('/');
console.log(arr);
It will split the string in array, forward slash will be used as separator.
This regular expression will return what's inbetween the slashes:
/\/(.*?)\//
Test code:
var regex = /\/(.*?)\//;
var str = './ankits/ankitt$.';
var result;
result = regex.exec(str);
alert(result[0]);
Short form:
"./ankits/ankitt$".match(/\/(.*?)\//)[1]
Try this
"./ankits/ankitt$".match(/\/([^\/]+)\//)[1]

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]);

jQuery - trim the variable

I have following example:
var VarFull = $('#selectror').attr('href') where .attr('href') = "#tabs1-1"
How can I trim that to "tabs1-1" ( without #)??
Any suggestions much appreciated.
Use substring:
var VarFull = $('#selectror').attr('href').substring(1);
You can use JavaScript's string replace(): https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace
var VarFull = $('#selectror').attr('href');
var trimmed = VarFull.replace('#','');
Edit:
This is a good article on JS string manipulation: http://www.quirksmode.org/js/strings.html
You could use replace -
var VarFull = $('#selectror').attr('href').replace('#','');
try this regEx with replace-
var VarFull = $('#selectror').attr('href').replace(/\#*/g, "");
it will replace all the # in your attr.
If it's certain that the url will contain # anyway, you can even split and take second element of array.
var trimmed=$('#selectror').attr('href').split("#")[1]
But don't use this if URL may not contain # otherwise you'll get an undefined error for trying to get index 1 of the array by split().
For example: ". / email#gmail.com, / . \"
Now trim characters at the beginning and end of the string:
email_new = email.replace(/\W+$/g, '').replace(/^\W+/g, ''); // output : email#gmail.com

how to extract string part and ignore number in jquery?

I have a string like foobar1, foobaz2, barbar23, nobar100 I want only foobar, foobaz, barbar, nobar and ignoring the number part.
If you want to strip out things that are digits, a regex can do that for you:
var s = "foobar1";
s = s.replace(/\d/g, "");
alert(s);
// "foobar"
(\d is the regex class for "digit". We're replacing them with nothing.)
Note that as given, it will remove any digit anywhere in the string.
This can be done in JavaScript:
/^[^\d]+/.exec("foobar1")[0]
This will return all characters from the beginning of string until a number is found.
var str = 'foobar1, foobaz2, barbar23, nobar100';
console.log(str.replace(/\d/g, ''));
Find some more information about regular expressions in javascript...
This should do what you want:
var re = /[0-9]*/g;
var newvalue= oldvalue.replace(re,"");
This replaces al numbers in the entire string. If you only want to remove at the end then use this:
var re = /[0-9]*$/g;
I don't know how to do that in JQuery, but in JavaScript you can just use a regular expression string replace.
var yourString = "foobar1, foobaz2, barbar23, nobar100";
var yourStringMinusDigits = yourString.replace(/\d/g,"");

Categories

Resources