- JavaScript Basics
- JavaScript Tutorial
- JavaScript: where to write
- JavaScript: how to display
- JavaScript: keywords
- JavaScript: comments
- JavaScript: variables
- JavaScript: operators
- JavaScript: data types
- JavaScript Conditional Statements
- JavaScript: if-else
- JavaScript: switch
- JavaScript: for loop
- JavaScript: while loop
- JavaScript: do-while loop
- JavaScript: break and continue
- JavaScript Popup Boxes
- JavaScript: alert box
- JavaScript: confirm box
- JavaScript: prompt box
- JavaScript Popular Topics
- JavaScript: functions
- JavaScript: innerHTML
- JavaScript: getElementById()
- JavaScript: getElementsByClassName()
- JavaScript: getElementsByName()
- JavaScript: getElementsByTagName()
- JavaScript: querySelector()
- JavaScript: querySelectorAll()
- JavaScript: document.write()
- JavaScript: console.log()
- JavaScript: boolean
- JavaScript: events
- JavaScript: Math object
- JavaScript: Math.random()
- JavaScript: Number()
- JavaScript: parseInt()
- JavaScript: parseFloat()
- JavaScript Arrays
- JavaScript: array
- JavaScript: find length of array
- JavaScript: add element at beginning
- JavaScript: add element at end
- JavaScript: remove first element
- JavaScript: remove last element
- JavaScript: get first index
- JavaScript: get last index
- JavaScript: reverse an array
- JavaScript: sort an array
- JavaScript: concatenate arrays
- JavaScript: join()
- JavaScript: toString()
- JavaScript: from()
- JavaScript: check if value exists
- JavaScript: check if array
- JavaScript: slice an array
- JavaScript: splice()
- JavaScript: find()
- JavaScript: findIndex()
- JavaScript: entries()
- JavaScript: every()
- JavaScript: fill()
- JavaScript: filter()
- JavaScript: forEach()
- JavaScript: map()
- JavaScript Strings
- JavaScript: string
- JavaScript: length of string
- JavaScript: convert to lowercase
- JavaScript: convert to uppercase
- JavaScript: string concatenation
- JavaScript: search()
- JavaScript: indexOf()
- JavaScript: search() vs. indexOf()
- JavaScript: match()
- JavaScript: match() vs. search()
- JavaScript: replace()
- JavaScript: toString()
- JavaScript: String()
- JavaScript: includes()
- JavaScript: substr()
- JavaScript: slice string
- JavaScript: charAt()
- JavaScript: repeat()
- JavaScript: split()
- JavaScript: charCodeAt()
- JavaScript: fromCharCode()
- JavaScript: startsWith()
- JavaScript: endsWith()
- JavaScript: trim()
- JavaScript: lastIndexOf()
- JavaScript Date and Time
- JavaScript: date and time
- JavaScript: Date()
- JavaScript: getFullYear()
- JavaScript: getMonth()
- JavaScript: getDate()
- JavaScript: getDay()
- JavaScript: getHours()
- JavaScript: getMinutes()
- JavaScript: getSeconds()
- JavaScript: getMilliseconds()
- JavaScript: getTime()
- JavaScript: getUTCFullYear()
- JavaScript: getUTCMonth()
- JavaScript: getUTCDate()
- JavaScript: getUTCDay()
- JavaScript: getUTCHours()
- JavaScript: getUTCMinutes()
- JavaScript: getUTCSeconds()
- JavaScript: getUTCMilliseconds()
- JavaScript: toDateString()
- JavaScript: toLocaleDateString()
- JavaScript: toLocaleTimeString()
- JavaScript: toLocaleString()
- JavaScript: toUTCString()
- JavaScript: getTimezoneOffset()
- JavaScript: toISOString()
- JavaScript Regular Expression
- JavaScript: regular expression
- JavaScript: RegEx . (dot)
- JavaScript: RegEx \w and \W
- JavaScript: RegEx \d and \D
- JavaScript: RegEx \s and \S
- JavaScript: RegEx \b and \B
- JavaScript: RegEx \0
- JavaScript: RegEx \n
- JavaScript: RegEx \xxx
- JavaScript: RegEx \xdd
- JavaScript: RegEx quantifiers
- JavaScript: RegEx test()
- JavaScript: RegEx lastIndex
- JavaScript: RegEx source
- JavaScript Programs
- JavaScript Programs
JavaScript \0 Metacharacter: RegEx Search Null Character
The JavaScript \0 metacharacter is used to find a null character in a string using JavaScript regular expressions. For example:
<!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>
Since the \0 is available at index number 18, the above JavaScript code produces 18 as output.
Note: The search() method is used to search for a substring (value) in a string using a regular expression.
The "\0" RegEx metacharacter in JavaScript is used in the above example to search for the null character in a string. The null character, represented by the escape sequence \0 or the Unicode character code U+0000, is a non-printable control character.
In this case, the myString variable is set to the string "JavaScript is Fun.\0Isn't it?" with a null character at the 16th index. The "pattern" variable is initialized with a regular expression that uses the \0 metacharacter to match the null character.
The string object's search() method is then called on the myString variable with the pattern variable as an argument. In this case, the search() method returns the index of the first occurrence of the pattern in the string, which is 16.
Finally, the innerHTML property of the paragraph element with ID "myPara" is set to the returned index value, resulting in "16" being displayed on the web page.
Now, before we wrap up our discussion of the "\0" metacharacter in JavaScript RegEx, I'd like to include one more example with a proper comment to explain each line of code in the program itself:
<!DOCTYPE html> <html> <body> <script> // Initialize a string variable containing a null character let myString = "This string contains a null character at position 5.\0The rest of the string is ignored."; // Create a regular expression to match the null character let pattern = /\0/; // Use the search() method to find the index of the first occurrence of the null character in the string let nullIndex = myString.search(pattern); // Output the index of the null character to the console console.log("The null character is at index " + nullIndex); </script> </body> </html>
The null character is at index 52
« Previous Tutorial Next Tutorial »