Return that word which has matching values

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”

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
  var str = "The rain in SPAIN stays mainly in the plain"; 
  var res = str.match(/ain/g);
  document.getElementById("demo").innerHTML = res;
}
</script>

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.

var res = str.match(/\w*ain/ig);
// ["rain", "SPAIN", "main", "plain"]
1 Like

thank youuuuuuuuuuuuuuuuuuuuuuuuuuu… you guys are the best

Hi and one more thing instead of entering the word in expression i want to call it from variable like
var findWord= ain;
For this do i have to use RegExp or can i do it with match function

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:

var findWord = 'ain';
var findReg = new RegExp('\\w*' + findWord, 'ig'); // '\\' escapes to just \
var res = str.match(findReg);
// ["rain", "SPAIN", "main", "plain"]

Note that if you ever need to change findWord, you’ll need to call new RegExp again to create an updated expression with that new value.

oh, thanks i got it

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 :wink: 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 :wink:

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.

Thank you very much i am able to extract numbers from the string but when i use (w*jan) i am unable to get the full word [january]

oops sorry its working now

Thank you guys

I am trying to get january using .match(/\w*jan\s+\d+/g)
but not working
Can you help on this

If you’re matching “january” using a partial of “jan”, then the extra part of the word you want to match is after “jan”, not before it. That being the case, you have to put the \w after “jan”.

"word january word".match(/jan\w*/);
// ["january"] 

ohkkk thanks boss its working
Thats all

Thanks