Search notes:

Javascript: RegExp object

Note: there are some String functions that work with regular expressions.

exec

<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>RegExp.exec</title>

  <script type="text/javascript">

    var out;

    function print(txt) {
      out.innerHTML += txt + "<br>";
    }

    function title(txt) {
      print ('<b>'+txt+'</b>');
    }
    
    function main() {
      out = document.getElementById('out');

      var re_number = /\d+/;
      title(re_number);
      var res = re_number.exec("foo 42 bar 19 baz 38 xyz");
      print (res); // 42

      var re_number_g = /\d+/g;
      title(re_number_g);
      var res;
      while (res = re_number_g.exec("foo 42 bar 19 baz 38 xyz")) {
        print (res);
        // 42
        // 19
        // 38
      }

      var re_with_parans = /\w+ +(\d)(\d)/g;
      while (res = re_with_parans.exec("foo 42 bar 19 baz 38 xyz")) {
        print (res);
        // foo 42,4,2
        // bar 19,1,9
        // baz 38,3,8
      }

      var re_doesnt_match = /blablabla/;

      res = re_doesnt_match.exec("foo 42 bar 19 baz 38 xyz");
      title(re_doesnt_match);
      print(res);
      // null

    }

  </script>

</head>
<body onload='main()';>

  <div id="out"></div>

  <p>Compare with <a href='../String/match.html'>String.match</a>.

</body>
</html>
Github repository about-javascript, path: /objects/RegExp/exec.html

replace

Apparently, replace is a method of the String object that can take a regular expression object as argument.
<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>JavaScript RegExp replace()</title>

  <script type="text/javascript">

    var out;

    function replaceNumberWithX(str) {

       var str_replaced = str.replace(/\d/g, 'X');

       out.innerHTML += "Turned <code>" + str  + "</code> into <code>" + str_replaced + "</code><br>";
    }

    function main() {

       out = document.getElementById('out');

       replaceNumberWithX("foo 2222 bar" );
       replaceNumberWithX("bar one baz"  );
       replaceNumberWithX("apples 4 ever");
      
    }
     
  </script>
</head>
<body onload="main();">

  <div id="out">
  </div>
   
  <p><code>replace()</code> is a <a href='../String/replace.html'>String Object method</a>. See also <a href='replace_with_function.html'>replace() with a RegExp and a callback function</a>.
  
</body>
</html>
Github repository about-javascript, path: /objects/RegExp/replace.html

replace with function

<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>JavaScript RegExp replace() with a function</title>

  <script type="text/javascript">

    var out;

    function makeLetterAfterUnderscoreUppercase(str) {

       var str_replaced = str.replace(/_(.)/, function(entire_match, paranthesis_match) { return "_" + paranthesis_match.toUpperCase(); });

       out.innerHTML += "Turned <code>" + str  + "</code> into <code>" + str_replaced + "</code><br>";
    }

    function main() {

       out = document.getElementById('out');

       makeLetterAfterUnderscoreUppercase("foo_bar_baz" );
       makeLetterAfterUnderscoreUppercase("AAA_BBB_CCC" );
      
    }
     
  </script>
</head>
<body onload="main();">

  <div id="out">
  </div>
   
  <p><code>replace()</code> is a <a href='../String/replace.html'>String Object method</a>. See also <a href='replace.html'>replace() with a RegExp</a>.
  
</body>
</html>
Github repository about-javascript, path: /objects/RegExp/replace_with_function.html

test

<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>JavaScript RegExp test()</title>

  <script type="text/javascript">

    var out;

    function testIfContainsNumber(str) {

       if ( /\d/.test(str)  )  {
          out.innerHTML += "<code>" + str  + "</code> contains a number<br>";
       }
       else {
          out.innerHTML += "<code>" + str + "</code> doesn't contain a number<br>";
       }
      
    }

    function main() {

       out = document.getElementById('out');

       testIfContainsNumber("foo"          );
       testIfContainsNumber("bar one baz"  );
       testIfContainsNumber("apples 4 ever");
      
    }
     
  </script>
</head>
<body onload="main();">

  <div id="out">
  </div>
   

</body>
</html>
Github repository about-javascript, path: /objects/RegExp/test.html

toString

<!DOCTYPE HTML>
<html>
<head>
  <title>Javasctipt RegExp.toString()</title>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">

  <script type="text/javascript">

    function init() {

      var out = document.getElementById('out');

      out.innerHTML = /(foo|bar).baz/.toString(); //     /(foo|bar).baz/
    }

  </script>
</head>
<body onload="init();">

  <div id="out"></div>

</body>
</html>



Github repository about-javascript, path: /objects/RegExp/toString.html

determineType

<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>Determine type of RegExp instance</title>

  <script type="text/javascript">
    
    function main() {
      re = /\d/;

      document.getElementById('type').innerHTML = Object.prototype.toString.call(re);

    }

  </script>

</head>
<body onload='main()';>

  The »type« of <code>/\d/</code> is: <code><span id='type'></span></code>

</body>
</html>
Github repository about-javascript, path: /objects/RegExp/determineType.html

See also

regular expressions
Javascript code snippets
objects
String methods operating on regular expressions

Index