they say multi flag regex works just like this :
if we were to give both the flags i and g to the regex /a/ , we’d write /a/ig (or equivalently /a/gi , since the order doesn’t matter).
but it’s not working in my case,
function check() { const regex = /[a-z]/gi; const strArray = textInput.value.match(regex); console.log(strArray); if (strArray.join('') === strArray.reverse().join('')) { result.innerText =${textInput.value} is a palindrome
; result.style.display = "block"; } else { result.innerText = ${textInput.value} is not a palindrome
; result.style.display = "block"; } }
sanity
January 29, 2024, 7:55am
2
Take a closer look at the strArray value after matching, it might look a bit different than what you expect.
For the future - please share your code as a text, instead of images.
thank you for the reply but i still don’t get it
sanity
January 29, 2024, 8:09am
4
What’s the value of strArray in the first case and what’s in the second?
You can add console.log(strArray) to be able to see that in console.
1 Like
but i want it to ignore the case i flage is there… why?
are you saying the problem is in the if statement condition
sanity
January 29, 2024, 8:54am
6
It ignored, but only as far as matching the letter. /[a-z]/i or /[a-z]/gi will match both uppercase and lowercase letters. It will not change the casing in what it returns.
1 Like
thank u ,I understand now
i actually found the solution when you said the first hint but i didn’t like it
function check() {
const regex = /[a-z]/ig;
const strArray = textInput.value.match(regex);
if (strArray.join('').toLowerCase() === strArray.reverse().join('').toLowerCase()) {
result.innerText = `${textInput.value} is a palindrome`;
result.style.display = "block";
} else {
result.innerText = `${textInput.value} is not a palindrome`;
result.style.display = "block";
}
}
system
Closed
July 29, 2024, 9:06pm
8
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.