JS - Check if an array position contains a word and then return another word
Ive got this problem where I need to check an arrayposition if the string contains this at the start:
as-se-gbg-XXXXXXXXXX-XXX-XX
I just need to check if the string contains the as-se-gbg stuff and then return it as Sweden, Gothenburg.
I tried to use this method:
if开发者_开发技巧(b == "as-se-gbg") {
return("Göteborg");
But the == operator is trying to match the hole string...
And the other stuff thats marked xxxxxx isnt anything important at all...
Id be glad if there is anyone that could help me with this problem :D
Best Regards,
EIGHTYFO
if (b.indexOf("as-se-gbg") === 0)
will be true if the pattern is at the beginning of the string. For more information see: indexOf
You need to use a regex match:
if(b.match(/^as-se-gbg-/) {
return "Göteborg";
}
精彩评论