Find a word with regex or an other solution


I’m trying to find a word on a sentence. The problem is that there 3 sentences, each one has it own word and can’t know when each one will be avaiable.

example :


Any way to solve this please?

What code have you tried?

1 Like

No code, just the name of the word.

I’m not good with regex

I suggest working through the Free Code Camp’s Regular Expressions curriculum section, so you can get better at using regex and attempt to solve the problem you have.

I am not even sure what you are trying to accomplish here. You say you are trying to find a word in a sentence, but you don’t say what the word is. Also, if you find the word in the sentence, then what?

1 Like

Hi @RandellDawson

So sorry for delay, sure i’m gonna follow the course.

What i’m looking for is there’s sentences, which each one of them has a word that i would use later. The thing is that they aren’t always shown on same time. Sometime i could have “word1” sometimes “word2” etc…

So what i’m looking to do, is a regex code that can find the words i need what ever sentence is shown. If it’s the first one, it should give me word1 and so on.

Hope this helps.

Thank you @RandellDawson

could you please provide three different examples? I am still having difficulties in understanding your needs

1 Like

Here’s the 3 messages i have each time i have only one of them shown on the app


what do you want the regex to find?

that is just a string, I need a complete example.

1 Like

What do you mean by complete example ?

For example i have this one

/ser/ss/dat1/loc/ word2

regexcode find word1 or word2 or word3

shows word2

something like this, i don’t how to use regex just learning it

So you want to find the last word of a string? Or you have a set of words and it is displayed the one found in the string?

1 Like

For example let’s say i have this sentence

/ser/ss/dat1/loc/ word1

I want to get the


and put it on a variable like myVar = “word1” for example.

But the thing here i can have the sentence with word1 or word2 or word3…

that’s my big issue, how to tell the regex code that the sentence might change and how to get me the “word” shown…

so you want the thing that is after last slash?

1 Like

Exactly, one issue i can’t know how many slashes. So i can’t focus on it

A regex like /(?<=\/)\w+$/ will look at the end of the string, match the alphanumeric characters (\w equal to [a-zA-Z0-9_]) after the slash and extract those

let re = /(?<=\/)\w+$/;

let str = "/ser/ss/dat1/loc/word1";

let word = str.match(re)[0];  // match() returns an array, so I am accessing the first item in the array

console.log(word); // will print "word1"
1 Like


Thank you so much this works me.

I’ve a small issue, there’s a word who looks like this “DATA.SSS”

At least the words i’m looking for have “/” before…

Sorry it would be so easy for you if i had put the real sentences… which i can’t sorry about that.

then instead of \w you need to create the class of characters yourself

For example, \w is equal to [a-zA-Z0-9_], so the full stop is missing, if you substitute the class with the square parenthesis you can add and remove characters as you wish - this is a pretty easy thing to do, so I will let you do it :wink: it is good practice with regex

1 Like


Thank you so much for you help and kidness.

This helps me resolve my issue.

For those who has the same issue as me here’s what i did


do you understand all the regex or do you want a breakdown?

I did unterstand a part of it.

$ as the end of the string

[a-zA-Z0-9_.] as the char i need to find

(?<=/) to start for the last string ? this one i’m not sure

(?<=\/) this is a lookbehind (you need the backward slash because it is needed to escape the forward slash). It says to start looking after /

[a-zA-Z0-9_.] the square parenthesis make this a character class, this matches from a to z, from A to Z, from 0 to 9, the underscore _ and full stop .

+ say to match one or more times

$ mark end of string

1 Like