This question already has answers here:
Find and get only number in string
(4 answers)
Closed 8 years ago.
I have the following string
/Date(1317772800000)/
I want to use a Javascript regular expression to extract the numerical portion of it
1317772800000
How is this possible?
That should be it
var numPart = "/Date(1317772800000)/".match(/(\d+)/)[1];
No need for regex. Use .substring() function. In this case try:
var whatever = "/Date(1317772800000)/";
whatever = whatever.substring(6,whatever.length-2);
This'll do it for you: http://regex101.com/r/zR0wH4
var re = /\/Date\((\d{13})\)\//;
re.exec('/Date(1317772800000)/');
=> ["/Date(1317772800000)/", "1317772800000"]
If you don't care about matching the date portion of the string and just want extract the digits from any string, you can use this instead:
var re = /(\d+)/;
re.exec('/Date(1317772800000)/')
["1317772800000", "1317772800000"]
Related
This question already has answers here:
How to tell if a string contains a certain character in JavaScript?
(21 answers)
Closed 4 years ago.
I'm a beginner in node.js so please do excuse me if my question is foolish. As we know we can use
var regex = /[ !##$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/g;
regex.test(str);
to check whether a string contains special charecters or not .But what I'm asking is how to check for only a particular charecter means how can I check only presence of #.
I tried to do
var regex = /[#]/g; regex.test(str).
Although it's not working but are there any other method of doing this?
You don't need a regex to find a single character in a string. You can use indexOf, like this:
var hasHashtag = str.indexOf('#') >= 0;
This returns true if the character is in the string.
Use includes to check the existence of # in your string. You don't actually require regex to do that.
var str = 'someSt#ring';
var res = str.includes('#');
console.log(res);
str = 'someSt#ri#ng';
res = str.includes('#');
console.log(res);
str = 'someString';
res = str.includes('#');
console.log(res);
Use indexOf
str.indexOf('#') >= 0;
This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 5 years ago.
now I begin learning regex, and I have set of string in format like "(9/13)", and I need get second number. I try this regex: /\(.*?[^\d]*(\d+?)\)/g, in online regex it works normally.
But here:
var d = "(9/13)";
var v = /\(.*?[^\d]*(\d+?)\)/g;
alert(d.match(v));
it returns "(9/13)" , what am I doing wrong?
const source = "(9/13)";
const re = /\/(\d+)\)/;
console.log('result', re.exec(source).pop())
you can use Regex.exec() to find the number
const source = "(9/13)";
const re = /\(\d+\/(\d+)\)/;
console.log(re.exec(source)[1])
This question already has answers here:
How to grab substring before a specified character in JavaScript?
(12 answers)
Closed 6 years ago.
I am attempting to take the city out of a city, state string. For instance, "Portland, OR" would need to equal "Portland". How can I use Javascript or a regular expression to do this?
var myString = "Portland, OR";
I want to extract everything up to the comma but not including the comma.
var city = "Portland, OR".split(",")[0];
With regex:
var regex = /^[^,]+/;
var city = regex.exec("Portland, OR");
This is the regex version
var result = myString.match(/^[^,]+/)
UPDATE
This one always returns a value without error
var result = String(myString || "").match(/^[^,]*/)[0]
This question already has answers here:
Create RegExps on the fly using string variables
(6 answers)
Closed 8 years ago.
I need to use data attribute for regex just as
<div data-regex="REGEX-HERE">
and then get the value by javascript and put in a variable. and then do a test like
var regex = $(this).attr("data-regex");
regex.test(name)
when I tried to use "^[\x20-\x7E]+$" for testing english character is didn't work.
Note when I tried this
var regex = /^[\x20-\x7E]+$/;
It worked.
Thanks in advance
You can do this:
var regex = new RegExp("^[\x20-\x7E]+$",""); // Modifiers on the tend
So finally:
var regex = new RegExp($(this).data("regex"));
regex.test(name)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I match multiple occurrences with a regex in JavaScript similar to PHP’s preg_match_all()?
I am trying to parse an xml document like this:
var str = data.match("<string>" + "(.*?)" + "</string>");
console.log(str);
I want to get all the elements between the [string] in an array but for some reason, it only returns the first string element found. Im not good with regular expressions so Im thinking this is just a small regex issue.
You want it to be global g
var str="<string>1</string><string>2</string><string>3</string>";
var n=str.match(/<string>(.*?)<\/string>/g);
//1,2,3
You have to form the RegEx adding a g to it like
/Regex/g