JavaScript \0 Metacharacter | RegEx Search Null Character

The JavaScript \0 metacharacter is used to find a null character in a string using JavaScript regular expression. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

  <p id="myPara"></p>

   <script>
      let myString = "JavaScript is Fun.\0Is not it?";
      let pattern = /\0/;
      document.getElementById("myPara").innerHTML = myString.search(pattern);
   </script>
   
</body>
</html>
Output

Since the \0 is available at index number 18, therefore the above JavaScript code produces 18 as output.

Note: The search() method is used to search a substring (value) in a string using regular expression.

JavaScript Online Test


« Previous Tutorial Next Tutorial »