检查字符串是否以某事开头? [英] Check if string begins with something?

查看:130
本文介绍了检查字符串是否以某事开头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Javascript StartsWith

我知道我可以做^ =看看是否有id从一些东西开始,我尝试使用它,但它没有用...基本上,我正在检索网址,我想为一个以某种方式开始的路径名元素设置一个类...

I know that I can do like ^= to see if an id starts with something, and I tried using that for this, but it didn't work... Basically, I'm retrieving the url and I want to set a class for an element for pathnames that start in a certain way...

所以,

var pathname = window.location.pathname;  //gives me /sub/1/train/yonks/459087

我想确保为每个以/ sub / 1开头的路径,我都可以为元素设置一个类......

I want to make sure that for every path that starts with /sub/1, I can set a class for an element...

if(pathname ^= '/sub/1') {  //this didn't work... 
        ... 


推荐答案

使用 stringObject.substring

if (pathname.substring(0, 6) == "/sub/1") {
    // ...
}

这篇关于检查字符串是否以某事开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆