WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Javascript Regular Expressions Example Tutorial


Javascript Regular Expressions Example Tutorial is today’s leading topic. The regular expression is the sequence of characters that form the search pattern.

Javascript Regular Expressions Example Tutorial

The regular expression also called regex is a way to work with strings, in a very performant way.

By formulating the regular expression with a special syntax, you can perform the following operations.

  • Search text in a string.
  • Replace substrings in a string.
  • Extract information from a string.

#Create a Node.js project.

Create one project directory using the following command.
mkdir regex
Now, inside the root of the project, create one file called the server.js and add the following code.
// server.js

const str = 'let us browse appdividend.com';

const l = str.search("appdividend");

console.log(l); // 14
The search() method searches the string for a specified value and returns the position of the match.
// server.js

const str = 'let us browse appdividend.com';

const l = str.search(/appdividend/);

console.log(l); // 14

#String replace() With a String

The replace() method replaces the specified value with another value in the string.
// server.js

const str = 'let us browse appdividend.com';

const replaced = str.replace('appdividend', 'youtube');

console.log(replaced);

Here, you will get the output like let us browse youtube.com. You can also do the same replace operation using the Regular Expression.
// server.js

const str = 'let us browse appdividend.com';

const replaced = str.replace(/appdividend/, 'youtube');

console.log(replaced);