Hi, i just saw an example on W3school that matches the letters with string
In below program the usee search for word “ain” and the function return three values because “rain ,spain, plain” has “ain” in ending the output looks like “ain, ain,ain”
I am wondering about how this program return the whole word which has “ain”
by that i mean the output be like “rain , spain , plain”

You can update the regular expression to include a pattern to preceded the “ain” which matches any other part of the word. “Word” characters are denoted by \w and you can indicate 0 or more of them with a *. The regex then becomes:

var res = str.match(/\w*ain/g);

As is, though, this will not match SPAIN because its all capital letters. If you want that to match too, you can include the i flag which makes the matching case-insensitive.

You would use both. match doesn’t change because that’s what you’re doing - finding matches. You would need to use RegExp to create your regular expression, however, since the literal form (/ ... / - which is a kind of shorthand for new RegExp(...)) doesn’t support variables.

The RegExp constructor takes two arguments, the first being a string representing what’s in between the two / in the literal form, and second a string of the flags that come after the second /. One thing to be careful of when using RexExp with strings is that characters like \ have to be escaped in strings because they are, themselves, escaped characters. This is important for your case because there’s a \w being used. So what we end up with is:

Well i am matching months initials like jan, feb …
and with your help i am able to get the full words.
i am wondering like after January there’s a date in a string next to it
like January 18 and somewhere January 18th ,
How could i get the date next to it?

That’s getting more complicated For that you have to not only match the following text, but also use a capture group to grab it.

Capture groups are denoted by parens ((...)) and will return in a match. The rules can get a little complicated but we can start with a simple example using your date. Lets use the string:

var str = "The best day in January is January 18th";

This has a month without a day and one with, and we’ll be sure to only capture the one with. We’ll assume a full month match for simplicity, so /January/ as a base:

var str = "The best day in January is January 18th";
var res = str.match(/January/g);
// ["January", "January"]

Next we need to match any numbers that follow January. We can use a similar shortcut there that we did with \w, this time using \d which matches only numerical digits. And for the space between, we use \s which matches any kind of whitespace.

Before, with \w we were matching 0 or more \w matches with *. We want something similar with our \d and \s except we want 1 or more. For that, we use + which acts like * but requires that at least one exist for a match. That gives us:

var res = str.match(/January\s+\d+/g);
// ["January 18"]

Now the tricky part is pulling out the number without the month. For this we use the capture group, wrapping the \d+ in parens. But for this to work with match we need to drop the global flag.

var res = str.match(/January\s+(\d+)/);
// ["January 18", "18"]

The capture group is showing a second match after the full expression match which relates to the capture group specified with the parens - our number. It’ll be in string form, but you can throw it into a parseInt to fix that

If you want global flag search behavior, you’ll want to use RegExp.exec instead. Examples in that link show how looping through exec can get you multiple captures throughout a single string.