JavaScript Regex Source Properties

JavaScript regex source property helps to return the source code or format of the regular expression. We can get the regular expression pattern used for the operation and validation.

Syntax

The following syntax is used to get the regular expression's pattern.

Supported Browsers

The browsers supported by RegExp source properties are given below:

  • Google Chrome
  • Edge
  • Firefox
  • Internet Explorer
  • Opera
  • Safari

Examples

The following examples work for the types of the regex data and its value.

Example 1:

The example shows the regex regex pattern for the source property. Here, we use regex directly with the source keyword.

Output

The output shows the regex source code of the object function.

JavaScript Regex Source Properties

Example 2:

The example shows the array function regex pattern for the source property. Here, we use regex with start and end match values using the source keyword.

Output

The output shows the regex source code of the function.

JavaScript Regex Source Properties

Example 3:

The example shows the string regex pattern for the source property. Here, we use regex with the new keyword and method format. The source property does not display modifiers of the pattern.

Output

The output shows the regex source code of the function.

JavaScript Regex Source Properties

Example 4:

The example shows the number function regex pattern for the source property. We use the regex value with a modifier, but the source code does not show modifiers.

Output

The output shows the regex source code of the object function.

JavaScript Regex Source Properties

Benefits

  • The regular expression structure can be generated as a string using the source property in certain cases.
  • Using the source property, we can duplicate a regular expression.
  • A new regular expression object can be constructed as the original object by filling the parameter of a new RegExp property and the value of a current RegExp object.
  • The source property helps debug regular expressions in code.

Disadvantages

  • We can easily change and modify the regex pattern in JavaScript.
  • The source format or pattern shows by property but not by modifiers.

Conclusion

The source property shows the regex code of JavaScript for complicated and lengthy operations. It helps developers to operate regex and its validation.






Latest Courses