From 306c53187f2b86cb19ff6a037bac4e0e629e5b4c Mon Sep 17 00:00:00 2001 From: Hasan Abdullah <37593075+HasanAbdullah31@users.noreply.github.com> Date: Tue, 25 Jun 2019 16:56:06 -0400 Subject: [PATCH] feat: add article for JavaScript String.search() (#36014) --- .../string/string-prototype-search/index.md | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/guide/english/javascript/standard-objects/string/string-prototype-search/index.md b/guide/english/javascript/standard-objects/string/string-prototype-search/index.md index 0ce74673cfd..18dd05fb7db 100644 --- a/guide/english/javascript/standard-objects/string/string-prototype-search/index.md +++ b/guide/english/javascript/standard-objects/string/string-prototype-search/index.md @@ -3,13 +3,24 @@ title: String.prototype.search --- ## String.prototype.search -This is a stub. Help our community expand it. +The `search()` method searches for a match between a regular expression and the given String object. If a match is found, `search()` returns the index of the first match; if not found, `search()` returns -1. -This quick style guide will help ensure your pull request gets accepted. +**Syntax** +```javascript +str.search(regexp) // regexp is a RegExp object +``` - +**Example** +```js +var x = "Hello World"; +var pattern1 = /[A-H][a-e]/; // first character between 'A' and 'H', second between 'a' and 'e' +var pattern2 = "world"; // the string 'world' +console.log(x.search(pattern1)); // 0 +console.log(x.search(pattern2)); // -1 +console.log(x.search("o")); // 4 +``` + +*Note*: `search()` implicitly converts a string argument to a RegExp object; in the example above, the string `world` in `pattern2` is converted to the regular expression `world` (and similarly for the string `o`). #### More Information: - - - +- [String.prototype.search() on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search)