Debug School

rakesh kumar
rakesh kumar

Posted on

Java Script Regular Expression

  • What are the JavaScript's built-in methods for performing pattern-matching ?
  • Explain different type of Regular Expression?
  • (i)Character Classes
  • (ii)Predefined Character Classes
  • (iii) Repetition Quantifiers
  • (iv)Position Anchors (Flags)
  • (v)Pattern Modifiers (Flags)
  • (vi)Alternation
  • (vii)Grouping
  • (viii)Word Boundaries
  • find particular words is available in a string or no of words matches in string ?
  • how to find and replace space with a hyphen character in a string using regular expression with the JavaScript replace() method:
  • how to find and replace sentence words in comma using split regular expression with the JavaScript replace() method:
  • how to display sentence words using for loop.
  • example will match only those names in the names array which start with the letter "J".
  • find particular words is available in a string or no of words matches in string with g and i modifiers ,Pattern Modifiers Flags?
  • find particular words is available in a string or no of words matches in string with g and i modifiers ,Pattern Modifiers Flags?
  • find any of 3 words matches in string?
  • JavaScript Match Words Starts or Ends with a Pattern Using Regular Expression
  1. What are the JavaScript's built-in methods for performing pattern-matching ?click here for Refrence Image description
  2. Explain different type of Regular Expression? (i)Character Classes (ii)Predefined Character Classes (iii) Repetition Quantifiers (iv)Position Anchors (Flags) (v)Pattern Modifiers (Flags) (vi)Alternation (vii)Grouping (viii)Word Boundaries click here for Refrence

Image description
Image description
Image description
Image description
Image description

  1. find particular words is available in a string or no of words matches in string ?click here for solution Image description

Image description

  1. how to find and replace space with a hyphen character in a string using regular expression with the JavaScript replace() method:
    click here for solution
    Image description

    1. how to find and replace sentence words in comma using split regular expression with the JavaScript replace() method: how to display sentence words using for loop click here for solution Image description
    2. how to find and replace sentence words in comma using split regular expression with the JavaScript replace() method: how to display sentence words using for loop click here for solution Image description 15.example will match only those names in the names array which start with the letter "J"

click here for solution

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Match Strings Beginning with Specific Characters Using Regular Expression</title>
</head>
<body>
    <script>
    var regex = /^J/;
    var names = ["James Bond", "Clark Kent", "John Rambo"];

    // Loop through names array and display matched names
    for(var name of names) {
        if(regex.test(name)) {
            document.write("<p>" + name + "</p>")
        }
    }
    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

output:

James Bond

John Rambo

  1. find particular words is available in a string or no of words matches in string with g and i modifiers (Pattern Modifiers (Flags)?click here for solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Match a Pattern in a Multiline String Using Regular Expression</title>
</head>
<body>
    <script>    
    var str = "Color red is more visible than \ncolor blue in daylight.";
    var regex1 = /^color/gi;
    var regex2 = /^color/gim;

    var matches = str.match(regex1); // pattern matching without multiline flag    
    document.write("<p>Found " + matches.length + " matches: " + matches + "</p>");
    console.log(matches);

    matches = str.match(regex2); // pattern matching with multiline flag
    document.write("<p>Found " + matches.length + " matches: " + matches + "</p>");
    console.log(matches);
    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

output:
Found 1 matches: Color

Found 2 matches: Color,color

  1. find any of 3 words matches in string?click here for solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Specify Alternative Version of a Pattern in a Regular Expression</title>
</head>
<body>
    <script>    
    var regex = /fox|dog|cat/;
    var str = "The quick brown fox jumps over the lazy dog.";
    var matches = str.match(regex);
    document.write("Matches the substring: " + matches);

    console.log(matches);
    // expected output: ["fox", index: 16, ...]
    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

output:
Matches the substring: fox

  1. JavaScript Match Words Starts or Ends with a Pattern Using Regular Expressionclick here for solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Match Words Starts or Ends with a Pattern Using Regular Expression</title>
</head>
<body>
    <script>   
    var regex = /(\bcar\w*)/g;
    var str = "Words begining with car: cart, carrot, cartoon. Words ending with car: oscar, supercar.";
    var replacement = '<b>$1</b>';

    // Highlights the words beginning with car in bold
    var result = str.replace(regex, replacement);
    document.write(result);
    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

output:
Words begining with car: cart, carrot, cartoon. Words ending with car: oscar, supercar.

Top comments (0)