Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9228135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:14:58+00:00 2026-06-18T05:14:58+00:00

I want to extract a string from another using JavaScript / RegExp. Here is

  • 0

I want to extract a string from another using JavaScript / RegExp.
Here is what I got:

var string = "wp-button wp-image-45 wp-label";
string.match(/(?:(?:.*)?\s+)?(wp-image-([0-9]+))(:?\s(?:.*)?)?/);
// returnes: ["wp-button ", "wp-image-45", "45", undefined]

I just want to have “wp-image-45”, so:

  • (Optional) any character
  • (Optional) followed by whitespace
  • (Required) followed by “wp-image-“
  • (Required) followed by any number
  • (Optional) followed by whitespacy
  • (Optional) followed by any character

What is missing here? Is it just some kind of bracketing or more?
I also tried

string.match(/(?:(?:.*)?\s+)?(?=(wp-image-([0-9]+)))(?=(:?\s(?:.*)?)?)/)

Edit: In the end I just want to have the number. But I’d also make this step in between.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:14:59+00:00Added an answer on June 18, 2026 at 5:14 am

    Regexps are not required to start matching at the beginning of the string, so your attempts to match whitespace and any character aren’t necessary. Also, “any character” includes whitespace (except newlines in certain modes).

    This should be all you need:

    string.match(/\bwp-image-(\d+)\b/)
    

    This will capture, for example, “wp-image-123” into matching group 0, and “123” into matching group 1.

    \b means “word boundary”, which ensures that you won’t match “abcwp-image-123def”. A word boundary is defined as any place where a non-word character is followed by a word character, or vice versa. A word character is consists of a letter, a number or an underscore.

    Also, I used \d instead of [0-9] simply out of convenience. They have slightly different meaning (\d also matches characters considered numbers in other languages), but that won’t make a difference in your case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to extract a query string from my URL using JavaScript, and I
I have a string like this: S=str1|str2|str3 I want extract another string from S
I have the following string from which I want to extract gcc-4.3.2 and C
I want to extract word after first 'from' in the following string- anti-CD24 from
I want to extract the first word of a variable from a string. For
I have this string: comment_1234 I want to extract the 1234 from the string.
I want to extract the value between the apostrophes, for example from this string:
I want to extract some numbers from the input line which is a string.
I want to parse the input string and extract the values from it. My
I want to get a url from a string. Heres my code to extract

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.