Solved

Regular expression that only finds last instance

Posted on 2016-09-09
1
76 Views
Last Modified: 2016-09-09
If I have strings such as these:
1.  AB-123
2.  AB 123
3.  AB-123-1
4.  AB 123-1
5.  AB XYZ-1

I need a regular expression that determine if the end contains /\-\d+$/ and only #3, #4, and #5 to qualify.
The problem I'm running into is that the above regex matches for #1, #3, #4, and #5 since they all have hyphens.
If there's a nice regex that can be used to solve this, that would be great.  Otherwise, I'm open to javascript, such as using the split function.  I've been trying that unsuccessfully, too.

var splitter = str.split(/\-\d+$/)

I just don't have a good way to tell if the string qualifies with /\-\d+$/ when there are possible hyphen instances earlier in the string.
0
Comment
Question by:mock5c
1 Comment
 
LVL 22

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 41791944
Assuming that 1., 2., 3., 4., 5. at the beginning of each example and the spaces after them are not part of the search string but the new line at the end of each line is, the following should work for a test condition.
/(\s|-)[\da-z]+-\d+$/

Open in new window

This searches for a space or dash followed by one or more alphanumeric characters followed by a dash and 1 or more numbers followed by a new line.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Provide the IDEA of External JS 1 34
Session on Html 8 36
Regular Expression needed 4 12
regex code to remove .cn and .com 2 6
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question