Getting the last number in a string (JavaScript) - javascript

var str = "7-Dec-1985"
var str = "12-Jan-1703"
var str = "18-Feb-1999"
How would I got about pulling just the year out of the string? I have tried a number of different RegExp but none seem to be working.
I would have expected re = new RegExp(/(\d+)\D*\z/); To have worked but sadly it did not.
Any suggestions would be very appreciated

this should do it
var year = str.match(/\d+$/)[0];

Since all of your str(s) use - as a separator, this will work for you:
var str = "7-Dec-1985",
arr = str.split('-'),
year = arr[2];
console.log(year);

I'd try: /.*(\d{4})$/
Test your regex's here: http://www.regular-expressions.info/javascriptexample.html

Related

Javascript regex matching syntax not working [duplicate]

This question already has an answer here:
Javascript regex match fails on actual page, but regex tests work just fine
(1 answer)
Closed 5 years ago.
I would like to test a string to ensure it has the following pattern:
asas/asas2/asas
The charatcers in between the slashes can be letters, digits or both.
I have a working example here, although i'm sure it could be improved.
Regex Example
But when tested in jsfiddle it doesn't work
Jsfiddle Example
var str = 'dfdfdf/dfdf/dfdf';
var patt = new RegExp("/(^\w+\/\w+\/\w+$)/g");
var res = patt.test(str);
alert(res);
The above code example always returns false.
Remove the quotes new RegExp(/(^\w+\/\w+\/\w+$)/g);
You just have to remove quotes.
var str = 'dfdfdf/dfdf/dfdf';
var patt = /(^\w+\/\w+\/\w+$)/g;
var res = patt.test(str);
alert(res);
Please try this code:
var str = 'dfdfdf/dfdf/dfdf';
var patt = new RegExp(/(^\w+\/\w+\/\w+$)/g);
var res = patt.test(str);
alert(res);
console.log(res);
Thanks

Get integer from javascript string

I've a strings with the values like:
share__43
share__153
share
share_section
How do I get the integer values like 43 or 153?
Try this
var regex = new RegExp(/([0-9]+)/g);
var test = "share__43 share__153 share share_section";
var match = regex.exec(test);
alert('Found: ' + match[1]);
Fiddle
Example, currently using a single string
var regex = /\d+/;
var str = "share__43";
alert (str.match(regex ));
Demo

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.

javascript - get two numbers from a string

I have a string like:
text-345-3535
The numbers can change.
How can I get the two numbers from it and store that into two variables?
var str = "text-345-3535"
var arr = str.split(/-/g).slice(1);
Try it out: http://jsfiddle.net/BZgUt/
This will give you an array with the last two number sets.
If you want them in separate variables add this.
var first = arr[0];
var second = arr[1];
Try it out: http://jsfiddle.net/BZgUt/1/
EDIT:
Just for fun, here's another way.
Try it out: http://jsfiddle.net/BZgUt/2/
var str = "text-345-3535",first,second;
str.replace(/(\d+)-(\d+)$/,function(str,p1,p2) {first = p1;second = p2});
var m = "text-345-3535".match(/.*?-(\d+)-(\d+)/);
m[1] will hold "345" and m[2] will have "3535"
If you're not accustomed to regular expressions, #patrick dw's answer is probably better for you, but this should work as well:
var strSource = "text-123-4567";
var rxNumbers = /\b(\d{3})-(\d{4})\b/
var arrMatches = rxNumbers.exec(strSource);
var strFirstCluster, strSecondCluster;
if (arrMatches) {
strFirstCluster = arrMatches[1];
strSecondCluster = arrMatches[2];
}
This will extract the numbers if it is exactly three digits followed by a dash followed by four digits. The expression can be modified in many ways to retrieve exactly the string you are after.
Try this,
var text = "text-123-4567";
if(text.match(/-([0-9]+)-([0-9]+)/)) {
var x = Text.match(/([0-9]+)-([0-9]+)/);
alert(x[0]);
alert(x[1]);
alert(x[2]);
}
Thanks.
Another way to do this (using String tokenizer).
int idx=0; int tokenCount;
String words[]=new String [500];
String message="text-345-3535";
StringTokenizer st=new StringTokenizer(message,"-");
tokenCount=st.countTokens();
System.out.println("Number of tokens = " + tokenCount);
while (st.hasMoreTokens()) // is there stuff to get?
{words[idx]=st.nextToken(); idx++;}
for (idx=0;idx<tokenCount; idx++)
{System.out.println(words[idx]);}
}
output
words[0] =>text
words[1] => 345
words[2] => 3535

Categories

Resources