1

I have several Javascript strings (using jQuery). All of them follow the same pattern, starting with 'ajax-', and ending with a name. For instance 'ajax-first', 'ajax-last', 'ajax-email', etc.

How can I make a regex to only grab the string after 'ajax-'?

So instead of 'ajax-email', I want just 'email'.

2
  • 1
    It should be mentioned whether or not you are using this in a jQuery call, as that places certain limits on the possible solutions to your situation. Commented Oct 11, 2011 at 19:45
  • 1
    @Code Jockey : Sorry about that. It is jQuery. Commented Oct 11, 2011 at 19:53

4 Answers 4

4

You don't need RegEx for this. If your prefix is always "ajax-" then you just can do this:

var name = string.substring(5);
Sign up to request clarification or add additional context in comments.

3 Comments

Good call. But when i try it, i get the following error. Object ajax-first has no method 'substring' Im new to javascript, so im not sure how to fix this... Heres my code. var $class = jQuery(this).parents('li').attr('class').match(/ajax-[\w]+/); alert( $class.substring(5) );
Though this answer represents the truth of the situation, it is *NOT* an answer to the question asked.
alert(jQuery(this).parents('li').attr('class').substring(5)); That should work.
2

Given a comment you made on another user's post, try the following:

var $li = jQuery(this).parents('li').get(0);
var ajaxName = $li.className.match(/(?:^|\s)ajax-(.*?)(?:$|\s)/)[1];

Demo can be found here


Below kept for reference only

var ajaxName = 'ajax-first'.match(/(\w+)$/)[0];
alert(ajaxName);

Use the \w (word) pattern and bind it to the end of the string. This will force a grab of everything past the last hyphen (assuming the value consists of only [upper/lower]case letters, numbers or an underscore).

The non-regex approach could also use the String.split method, coupled with Array.pop.

var parts = 'ajax-first'.split('-');
var ajaxName = parts.pop();
alert(ajaxName);

5 Comments

this would break if the string were ajax-first-name or similar, by only capturing the last group of letters, numbers, or underscores.
@CodeJockey: Agreed, which is why I included a disclaimer.
Even after reviewing your answer, I still don't see a disclaimer that addresses my concern that your code grabs the string after the last hyphen, rather than "grab the string after 'ajax-'?" as the asker specified
Would you mind linking to or quoting the other user's post (for context)?
@CodeJockey: It was the comment (with code sample) made in a comment to @Styrr's answer. After I saw it trying to be parsed from the className property, and may not be a class name on-its-own, I decided to take a different regex approach.
0

you can try to replace ajax- with ""

2 Comments

Came across this when reviewing; it was flagged for low-quality. Might want to elaborate.
I think many users will appreciate if you could add more details, say, why this is possible and how it solves the problem.
0

I like the split method @Brad Christie mentions, but I would just do

function getLastPart(str,delimiter) {
  return str.split(delimiter)[1];
}

This works if you will always have only two-part strings separated by a hyphen. If you wanted to generalize it for any particular piece of a multiple-hyphenated string, you would need to write a more involved function that included an index, but then you'd have to check for out of bounds errors, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.