search()

ES3+

Executes a search for a match between a regular expression and this string.

Syntax

string.search(regexp)

Parameters

regexp RegExp

A regular expression object

Return Value

number

The index of the first match, or -1 if not found

Examples

JavaScript
const str = 'Hello World';
console.log(str.search(/World/));
console.log(str.search(/xyz/));
Output:
// 6 -1

📌 When to Use

Use search() when you need the position of a pattern match using regex capabilities like case-insensitivity or character classes. For simple string searches, indexOf() is faster and more straightforward.

⚠️ Common Mistakes

Using search() for simple string matching when indexOf() would be more efficient and clearer.

Not understanding that search() ignores the global flag - it always returns the first match position only.

Expecting search() to return match details like match() does - search() only returns the index, not the matched text.

✅ Best Practices

Use search() with /i flag for case-insensitive position finding: str.search(/hello/i) finds "Hello" or "HELLO".

Prefer indexOf() for literal string searches, as it is faster and more readable for simple cases.

When you need both the position and the matched text, use match() or exec() instead of search().

⚡ Performance Notes

search() compiles the regex each call if passed as a literal. For performance-critical code with repeated searches, cache the RegExp object. search() is generally slower than indexOf() for simple string matching due to regex overhead.

🌍 Real World Example

Pattern-Based Position Finding

search() is useful when you need regex capabilities like case-insensitivity, character classes, or pattern matching to find where a pattern first occurs in a string.

// Case-insensitive search for keyword position
function findKeywordPosition(text, keyword) {
  const pattern = new RegExp(keyword, 'i');
  return text.search(pattern);
}

console.log(findKeywordPosition('Welcome to JavaScript!', 'javascript')); // 11

// Find first digit position in string
function findFirstDigit(str) {
  return str.search(/\d/);
}

console.log(findFirstDigit('Order ABC-123')); // 10
console.log(findFirstDigit('No digits here')); // -1

// Check if string starts with specific pattern (similar to startsWith but with regex)
function startsWithPattern(str, pattern) {
  const regex = new RegExp('^' + pattern);
  return str.search(regex) === 0;
}

console.log(startsWithPattern('Hello World', 'Hello')); // true
console.log(startsWithPattern('Hello World', 'World')); // false

// Find position of first special character
function findFirstSpecialChar(str) {
  return str.search(/[!@#$%^&*(),.?":{}|<>]/);
}

console.log(findFirstSpecialChar('Hello, World!')); // 5 (the comma)

// Validate string format by checking pattern position
function hasValidPrefix(code) {
  // Code must start with 2-3 letters followed by digits
  return code.search(/^[A-Z]{2,3}\d/) === 0;
}

console.log(hasValidPrefix('AB123')); // true
console.log(hasValidPrefix('ABC456')); // true
console.log(hasValidPrefix('123ABC')); // false

Related Methods