The match() method returns an array which contains a matching sub string based on regular expression.
It’s syntax looks like –
Willing to learn How to create a regular expression click here.
let string = “1.JS 2.Startup”;
In the second step, create an another variable matches which contain an array return from match method.
let matches = string.match(/\d+/g);
Finally, we are checking the value of matches variable using console log.
Any guess, what should be the output of above statement ?
Now, you know the answer is [“1”, “2”]. But why, what does that matching. Let me explain.
So, as we pass a regular expression “/\d+/g” which extract all from given string.
And match method work on string so, match string return as string in array.
That’s why when we apply match method on string variable it return an array which contain two element only.
string.match(/\d+/g) = [“1”, “2”]
At last, That’s why we get an answer “[“1”, “2”]“.
Let me know in the comment section, what you think about it.
I hope you understand the concept and logic behind it.
But wait, i have something more for you –
- JS Quiz – compare the self value with data type
- JS Quiz – can NaN be really equal to NaN
And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page
If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.