Welcome to the community, @ShinKu! :grinning_face_with_big_eyes: I’ve got a couple questions right out of the gate:
- 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.
- 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—r^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:
`c^a-zA-Z0-9]*${subcategoryName}Z^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.
Welcome to the community, @ShinKu! :grinning_face_with_big_eyes: I’ve got a couple questions right out of the gate:
- 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.
- 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—r^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:
`c^a-zA-Z0-9]*${subcategoryName}Z^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.
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.