Solved

Regular expression that only finds last instance

Posted on 2016-09-09
1
47 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 21

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now