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 8025143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:04:17+00:00 2026-06-04T23:04:17+00:00

This is a noob question Suppose I search a string S for a pattern

  • 0

This is a noob question

Suppose I search a string S for a pattern P. Now I would like to display a substring of the string, which surrounds P. The substring should be only one line (i.e. N characters) and contain whole words. How would you code it in JavaScript?

For example:
Let S = “Hello world, welcome to the universe”, P = “welcome”, and N = 15. The naive solution gives “ld, welcome to ” (adding 4 chars before and after P). I would like to “round” it up to “world, welcome to”.

Can regular expressions help me here?

  • 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-04T23:04:19+00:00Added an answer on June 4, 2026 at 11:04 pm

    Here is the regular expression you wanted:

    /\s?([^\s]+\swelcome\s[^\s]+)\s?/i    //very simple, no a strange bunch of [] and {}
    

    Explanation:

    What you are trying to match is actually

     ” world, welcome to “

    without the spaces before and after, therefore:

    \s?       //the first space (if found)
    (         //define the string position you want
    [^\s]+    //any text (first word before "welcome", no space)
    \s        //a space
    welcome   //you word
    \s        //a space
    [^\s]+    //the next world (no space inside)
    )         //that's it, I don't want the last space
    \s?       //the space at the end (if found)
    

    Applying:

    function find_it(p){
        var s = "Hello world, welcome to the universe",
            reg = new RegExp("\\s?([^\\s]+\\s" + p + "\\s[^\\s]+)\\s?", "i");
    
        return s.match(reg) && s.match(reg)[1];
    }
    
    find_it("welcome");   //"world, welcome to"
    
    find_it("world,");    //"Hello world, welcome"
    
    find_it("universe");  //null (because there is no word after "universe")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may sound like a noob question, because it is. I have tried for
This is going to seem like a noob question, sorry. I can't get my
Sorry for the very noob question. Let's suppose I have an enum like so
I'm pretty new at bash so this is a pretty noob question.. Suppose I
This is a noob question. I am trying to use node.js as a JavaScript
This is a noob question, but I forked a project and made few edits
This is a noob question: How to parse a date in format YYYYmmdd without
This is a noob question from someone who hasn't written a parser/lexer ever before.
This is a noob question, but I can't get Valum's Uploader to work no
Sorry this is a noob question but if I only need some initial data

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.