开发者

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";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜