检查字符串是否以某物开头?(复制)

时间:2022-03-16 22:08:36

Possible Duplicate:
Javascript StartsWith

可能的重复:Javascript StartsWith

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

我知道我可以做喜欢^ =一个id是否开始,我试着用,但它没有工作…基本上,我正在检索url,我想为路径名的元素设置一个类,以某种方式开始…

So,

所以,

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

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

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

6 个解决方案

#1


349  

Use stringObject.substring

使用stringObject.substring

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

#2


172  

String.prototype.startsWith = function(needle)
{
    return this.indexOf(needle) === 0;
};

#3


82  

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

可以使用string.match()和正则表达式:

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

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

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

#4


34  

A little more reusable function:

更可重复使用的功能:

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

#5


22  

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.

首先,让我们扩展字符串对象。多亏了Ricardo Peres的原型,我认为在使它更可读的情况下,使用变量'string'比'needle'更有效。

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
}

#6


1  

Have a look at JavaScript substring() method.

看看JavaScript子字符串()方法。

#1


349  

Use stringObject.substring

使用stringObject.substring

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

#2


172  

String.prototype.startsWith = function(needle)
{
    return this.indexOf(needle) === 0;
};

#3


82  

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

可以使用string.match()和正则表达式:

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

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

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

#4


34  

A little more reusable function:

更可重复使用的功能:

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

#5


22  

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.

首先,让我们扩展字符串对象。多亏了Ricardo Peres的原型,我认为在使它更可读的情况下,使用变量'string'比'needle'更有效。

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
}

#6


1  

Have a look at JavaScript substring() method.

看看JavaScript子字符串()方法。