32
loading...
This website collects cookies to deliver better user experience
Regular Expressions
..test()
method. Let's get started with that. 👇🏻 .test()
method takes the regex, applies it to a string (which is placed inside the parentheses), and returns true if your pattern finds something similar to the given regex and false otherwise. regex.test(string)
let codingIsHiding = "Somewhere coding is hiding in this text.";
let codingRegex = /coding/;
let result = codingRegex.test(codingIsHiding);
console.log(result);
//output: true
alternation
or OR operator: |
.let myString = "Swarnali loves rain and snow.";
let weather = /rain|cloud|sun|snow|heat/ ;
let pet = /cats|dogs|birds|fishes/
let weatherResult = weather.test(myString);
let petResult = pet.test(myString);
console.log(weatherResult); //output: true
console.log(petResult); //output: false