# Return that word which has matching values

**URL:** <https://forum.kirupa.com/t/return-that-word-which-has-matching-values/639295>\
**Category:** Uncategorized\
**Created:** [January 17, 2019, 8:28am UTC](https://forum.kirupa.com/t/return-that-word-which-has-matching-values/639295 "2019-01-17T08:28:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sukhwinder\_Saini](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sukhwinder\_Saini](https://forum.kirupa.com/u/Sukhwinder_Saini)\
**Post date:** [January 17, 2019, 8:28am UTC](https://forum.kirupa.com/t/return-that-word-which-has-matching-values/639295/1 "2019-01-17T08:28:04Z")

</div>

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”

```auto
<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>

```

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [January 17, 2019, 2:22pm UTC](https://forum.kirupa.com/t/return-that-word-which-has-matching-values/639295/2 "2019-01-17T14:22:52Z")

</div>

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:

```auto
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.

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

```

---

<div class="post-metadata">

**Author:** ![Sukhwinder\_Saini](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sukhwinder\_Saini](https://forum.kirupa.com/u/Sukhwinder_Saini)\
**Post date:** [January 18, 2019, 5:16pm UTC](https://forum.kirupa.com/t/return-that-word-which-has-matching-values/639295/3 "2019-01-18T17:16:46Z")

</div>

thank youuuuuuuuuuuuuuuuuuuuuuuuuuu… you guys are the best

---

<div class="post-metadata">

**Author:** ![Sukhwinder\_Saini](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sukhwinder\_Saini](https://forum.kirupa.com/u/Sukhwinder_Saini)\
**Post date:** [January 18, 2019, 5:43pm UTC](https://forum.kirupa.com/t/return-that-word-which-has-matching-values/639295/4 "2019-01-18T17:43:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [January 18, 2019, 6:11pm UTC](https://forum.kirupa.com/t/return-that-word-which-has-matching-values/639295/5 "2019-01-18T18:11:35Z")

</div>

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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Sukhwinder\_Saini](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sukhwinder\_Saini](https://forum.kirupa.com/u/Sukhwinder_Saini)\
**Post date:** [January 18, 2019, 6:42pm UTC](https://forum.kirupa.com/t/return-that-word-which-has-matching-values/639295/6 "2019-01-18T18:42:33Z")

</div>

oh, thanks i got it

---

<div class="post-metadata">

**Author:** ![Sukhwinder\_Saini](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sukhwinder\_Saini](https://forum.kirupa.com/u/Sukhwinder_Saini)\
**Post date:** [January 18, 2019, 6:50pm UTC](https://forum.kirupa.com/t/return-that-word-which-has-matching-values/639295/7 "2019-01-18T18:50:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [January 18, 2019, 8:33pm UTC](https://forum.kirupa.com/t/return-that-word-which-has-matching-values/639295/8 "2019-01-18T20:33:56Z")

</div>

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:

```auto
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:

```auto
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:

```auto
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.

```auto
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) instead. Examples in that link show how looping through exec can get you multiple captures throughout a single string.

---

<div class="post-metadata">

**Author:** ![Sukhwinder\_Saini](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sukhwinder\_Saini](https://forum.kirupa.com/u/Sukhwinder_Saini)\
**Post date:** [January 19, 2019, 9:06pm UTC](https://forum.kirupa.com/t/return-that-word-which-has-matching-values/639295/9 "2019-01-19T21:06:14Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Sukhwinder\_Saini](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sukhwinder\_Saini](https://forum.kirupa.com/u/Sukhwinder_Saini)\
**Post date:** [January 19, 2019, 9:23pm UTC](https://forum.kirupa.com/t/return-that-word-which-has-matching-values/639295/10 "2019-01-19T21:23:51Z")

</div>

oops sorry its working now

Thank you guys

---

<div class="post-metadata">

**Author:** ![Sukhwinder\_Saini](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sukhwinder\_Saini](https://forum.kirupa.com/u/Sukhwinder_Saini)\
**Post date:** [January 19, 2019, 9:33pm UTC](https://forum.kirupa.com/t/return-that-word-which-has-matching-values/639295/11 "2019-01-19T21:33:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [January 19, 2019, 10:07pm UTC](https://forum.kirupa.com/t/return-that-word-which-has-matching-values/639295/12 "2019-01-19T22:07:59Z")

</div>

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”.

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

```

---

<div class="post-metadata">

**Author:** ![Sukhwinder\_Saini](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sukhwinder\_Saini](https://forum.kirupa.com/u/Sukhwinder_Saini)\
**Post date:** [January 19, 2019, 10:20pm UTC](https://forum.kirupa.com/t/return-that-word-which-has-matching-values/639295/13 "2019-01-19T22:20:41Z")

</div>

ohkkk thanks boss its working  
Thats all

Thanks
