1

I have spent time here Regex101 and here RegExr but still can't figure out how to do the following:

Example String: "input_u_s13p_11_backup_person"

What is consistent in every string: "input_u_s13p_"

Before: input_u_s13p_11_backup_person
After:  u_backup_person
Before: input_u_s13p_6_a_little_thing
After:  u_a_little_thing
Before: input_u_s13p_10_name
After:  u_name
Before: input_u_s13p_6_next_process_1
After:  u_next_process_1

Basically, I need all "input_u_s13p_" ripped out, and the "??" followed by it too. ?? = 1 or 2 digit number

5
  • 1
    And what is your attempt? Commented Jun 20, 2017 at 12:47
  • input_u_s13p_\d{1,2} is a match for what you are looking for, is that what you need? Commented Jun 20, 2017 at 13:07
  • @NiettheDarkAbsol var str = 'input_u_s13p_10_backup_person'; str = str.replace('input_u_s13p_', ''); str = str.replace(/[^_]*/, ''); str = 'u' + str; Commented Jun 20, 2017 at 13:08
  • @sniperd Yes, that's much better than what I had. Thank you, your answer was perfect. Commented Jun 20, 2017 at 13:10
  • cool! I'll throw that in as an answer and if you could accept it that would be great Commented Jun 20, 2017 at 13:29

3 Answers 3

1
input_u_s13p_\d{1,2} 

That should be the regex you are looking for. Nice and simple. The \d is numbers and the {1,2} means one or two of them

Sign up to request clarification or add additional context in comments.

Comments

1

Can you try the following regex:

input_u_[a-z\d]+_\d{1,2}_

Explanation

  • input_u_ matches the characters input_u_ literally (case sensitive)
  • [\w\d]+_ every digit/character string followed by a _
  • \d{1,2}_ one or two digits followed by a _

var arr = ['input_u_s13p_10_backup_person', 'input_u_s13p_6_a_little_thing', 'input_u_s13p_10_name', 'input_u_s13p_6_next_process_1']; 

arr.forEach(function(str) {
    str = str.replace(/input_u_[\w\d]+_\d{1,2}_/g, ''); 
    str = 'u_' + str;
    console.log(str);
});

Comments

1

I think this regex can help you input_(.*?_).*\d_(.*) :

regex demo

1 Comment

Very, very nice. Thanks for the regex demo link too!

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.