检查字符串是否以某些内容开头?

发布于 2024-08-11 08:32:18 字数 372 浏览 4 评论 0原文

我知道我可以像 ^= 那样查看 id 是否以某些内容开头,并且我尝试使用它来实现此目的,但它不起作用。基本上,我正在检索一个 URL,并且想为以某种方式开头的路径名的元素设置一个类。

示例:

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

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

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

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 a URL and I want to set a class for an element for path names that start in a certain way.

Example:

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

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... 
        ... 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

裂开嘴轻声笑有多痛 2024-08-18 08:32:18

使用 stringObject.substring

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

Use stringObject.substring

if (pathname.substring(0, 6) == "/sub/1") {
    // ...
}
神仙妹妹 2024-08-18 08:32:18
String.prototype.startsWith = function(needle)
{
    return this.indexOf(needle) === 0;
};
String.prototype.startsWith = function(needle)
{
    return this.indexOf(needle) === 0;
};
翻身的咸鱼 2024-08-18 08:32:18

您可以使用 string.match() 和正则表达式对于这一点,

if(pathname.match(/^\/sub\/1/)) { // you need to escape the slashes

string.match() 将返回一个匹配子字符串的数组(如果找到),否则null

You can use string.match() and a regular expression for this too:

if(pathname.match(/^\/sub\/1/)) { // you need to escape the slashes

string.match() will return an array of matching substrings if found, otherwise null.

小女人ら 2024-08-18 08:32:18

更可重用的功能:

beginsWith = function(needle, haystack){
    return (haystack.substr(0, needle.length) == needle);
}

A little more reusable function:

beginsWith = function(needle, haystack){
    return (haystack.substr(0, needle.length) == needle);
}
城歌 2024-08-18 08:32:18

首先,让我们扩展字符串对象。感谢 Ricardo Peres 的原型,我认为使用变量“字符串”比“针”效果更好,使其更具可读性。

String.prototype.beginsWith = function (string) {
    return(this.indexOf(string) === 0);
};

然后你就这样使用它。警告!使代码极具可读性。

var pathname = window.location.pathname;
if (pathname.beginsWith('/sub/1')) {
    // Do stuff here
}

First, lets extend the string object. Thanks to Ricardo Peres for the prototype, I think using the variable 'string' works better than 'needle' in the context of making it more readable.

String.prototype.beginsWith = function (string) {
    return(this.indexOf(string) === 0);
};

Then you use it like this. Caution! Makes the code extremely readable.

var pathname = window.location.pathname;
if (pathname.beginsWith('/sub/1')) {
    // Do stuff here
}
始终不够爱げ你 2024-08-18 08:32:18

查看 JavaScript substring() 方法。

Have a look at JavaScript substring() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文