Search notes:

JavaScript: Find the position of regular expression in a string with search()

String.search returns the (character) position of the (first) matched regular expression.
<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>String.search()</title>

  <script type="text/javascript">
    
    function main() {

      reNum = /\d/; // Search for a number

      str = 'foo 42 bar';

      posFirstNum = str.search(reNum);
      
      var out = document.getElementById('out');

      out.innerHTML = "The position of the first number in <b>" + str + "</b> is: " + posFirstNum;

    }

  </script>

</head>
<body onload='main()';>
  <div id="out"></div>
</body>
</html>
Github repository about-javascript, path: /objects/String/search.html

See also

match(…) returns an array of matched strings.

Index