Help

Re: My RegEx is not working in the String.Search function

Solved
Jump to Solution
965 0
cancel
Showing results for 
Search instead for 
Did you mean: 
ShinKu
4 - Data Explorer
4 - Data Explorer

Hi,

I would like to ask what is wrong with this script below. What I’m trying to achieve is search a particular word on a given string and make sure that the word is not misspelled by not having any additional letter or numbers at the beginning or end of the word (Special characters are allowed. Case sensitive search). What happens with this script is, it fails to search for the given word even if nothing is added before or after the word.

I have tried my RegEx on online testing tools and it actually works fine. If you guys know any alternative for my scenario please do let me know! Thank you very much.

let subcategoryName = currentSubcategoryRecord.name;
let keyword = new RegExp(`[^a-zA-Z0-9]${subcategoryName}[^a-zA-Z0-9]`);

isProductTypeExists = checkRecord.search(keyword);

if (isProductTypeExists == 0)
{
    //Product exists!
}
// -1 is returned for no match found.
1 Solution

Accepted Solutions
Justin_Barrett
18 - Pluto
18 - Pluto

Welcome to the community, @ShinKu! :grinning_face_with_big_eyes: I’ve got a couple questions right out of the gate:

  1. What is checkRecord? Is that a string? A record from a query? Something else? You didn’t include earlier parts of your code, so it’s tough to know how to interpret that.
  2. You’re missing a let or const keyword before defining isProductTypeExists. Is that variable defined earlier in the script, or is this the first time that it’s used?

Next, the check that you’re doing in the if statement will only be true if the keyword is at the very start of the string. The number returned is an index value. Strings (and other things) are indexed using a 0-based indexing scheme, so an index of 0 represents the first character of the string. A more appropriate test to see if it’s anywhere in the string would be:

if (isProductTypeExists !== -1)

Finally, there’s the expression itself. One problem with this combo on either side of your keyword—[^a-zA-Z0-9]—is that it’s still looking for a character of some kind. It can’t be a letter or number, but some other character must be there for the search to match.

In order to match the keyword regardless of whether a leading/trailing character exists, change the expression to this:

`[^a-zA-Z0-9]*${subcategoryName}[^a-zA-Z0-9]*`

The asterisk after each of the square-brace blocks means that there can be zero or more of the preceding token—in this case, zero or more non-alphanumeric characters—in those positions. Omitting the asterisks means that at least one non-alphanumeric character must exist—both before and after—for the match to succeed.

See Solution in Thread

3 Replies 3
Justin_Barrett
18 - Pluto
18 - Pluto

Welcome to the community, @ShinKu! :grinning_face_with_big_eyes: I’ve got a couple questions right out of the gate:

  1. What is checkRecord? Is that a string? A record from a query? Something else? You didn’t include earlier parts of your code, so it’s tough to know how to interpret that.
  2. You’re missing a let or const keyword before defining isProductTypeExists. Is that variable defined earlier in the script, or is this the first time that it’s used?

Next, the check that you’re doing in the if statement will only be true if the keyword is at the very start of the string. The number returned is an index value. Strings (and other things) are indexed using a 0-based indexing scheme, so an index of 0 represents the first character of the string. A more appropriate test to see if it’s anywhere in the string would be:

if (isProductTypeExists !== -1)

Finally, there’s the expression itself. One problem with this combo on either side of your keyword—[^a-zA-Z0-9]—is that it’s still looking for a character of some kind. It can’t be a letter or number, but some other character must be there for the search to match.

In order to match the keyword regardless of whether a leading/trailing character exists, change the expression to this:

`[^a-zA-Z0-9]*${subcategoryName}[^a-zA-Z0-9]*`

The asterisk after each of the square-brace blocks means that there can be zero or more of the preceding token—in this case, zero or more non-alphanumeric characters—in those positions. Omitting the asterisks means that at least one non-alphanumeric character must exist—both before and after—for the match to succeed.

Hi @Justin_Barrett!

I updated my if statement just like what you said below:

if (isProductTypeExists !== -1)

and this worked for me. I was using Console.Debug to check for the value of isProductTypeExists but it is always showing me zero when the keyword matches the given string so I thought that it was always the case regardless where the matching keyword in the string is located. Not to mention, I’m not that familiar with these operator !== and === that much.

I didn’t apply asterisk to the expression because I expect that there will always be non-alphanumeric characters before and after my keyword, therefore I maintained it that way.

Thank you for your help.

They compare types as well as values; e.g. “1” and 1 will be seen as equal with ==, but not with === because one is a string and the other is a number. Long story short, it’s usually better to use === vs == because it’s a more precise comparison.