Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

74
Views
JavaScript: Search string from position

JavaScript has two useful methods, both which nearly do what I need.

  • String.prototype.indexOf() will search for a substring and return its position. It has an optional position parameter which is the starting point of the search, so you can easily find the next one.
  • String.prototype.search() will search a string using a regular expression and return its position. However, as far as I can tell, it doesn’t allow a starting position, so it always searches from the start.

Is there anything which allows me to find the position using a regular expression which does allow for a starting position?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You could do a String#slice in advance to get rid of unwanted parts and then take String#search.

function search(string, regexp, from = 0) {
    const index = string.slice(from).search(regexp);
    return index === -1
        ? -1
        : index + from;
}
about 4 years ago · Santiago Trujillo Report

0

In order to get the position of the original position of the search result you could use Nina Scholz' approach and then re-add the characters removed from the start. For Instance:

const str = "Hello World!";
let start = 5;
const output = str.substring(start).search(/World/g) + start;
console.log(output); //6

Note: Be careful with cases where .search() returns -1.

about 4 years ago · Santiago Trujillo Report

0

The other answer is the way to go as its much clearer what the intention is, but if you had to use a regular expression, you could do it like this:

// This string should not include characters that have special
// meaning in regular expressions / should be escaped.
const inputString = "foo bar baz bar";
const offset = 8;
const searchString = "bar";
inputString.search(new RegExp(`(?=.{${offset}}${searchString}`));

//  (?=             make sure ${searchString} ("bar") 
//                  is preceded by:
//  .               anything
//  {$(a number)}   at least this many times:
//                  {8} in this example.

// The (?=A)B positive lookahead won't be included in the result,
// so you don't need to plus or minus the offset from the result:

// Example:
const str = 'aabbcc aabbcc aabbcc';
               ^      ^      ^
               2      9      16

str.search(new RegExp(`(?<=.{${0}})bb`));
// Return 2

str.search(new RegExp(`(?<=.{${4}})bb`));
// Returns 9

str.search(new RegExp(`(?<=.{${10}})bb`));
// Returns 16

str.search(new RegExp(`(?<=.{${1000}})bb`));
// Returns -1

str.search(new RegExp(`(?<=.{${10}})doesntexist`));
// Returns -1
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!