Javascript 如何知道字符串以 jQuery 中的特定字符串开始/结束?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3715309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:50:00  来源:igfitidea点击:

How to know that a string starts/ends with a specific string in jQuery?

javascriptjquerystring

提问by Naveed

I want to know if a string starts with the specified character/string or ends with it in jQuery.

我想知道一个字符串是否以指定的字符/字符串开头或在 jQuery 中以它结尾。

For Example:

例如:

var str = 'Hello World';

if( str starts with 'Hello' ) {
   alert('true');
} else {
   alert('false');
}

if( str ends with 'World' ) {
   alert('true');
} else {
   alert('false');
}

If there is not any function then any alternative ?

如果没有任何功能,那么还有其他选择吗?

回答by Luká? Lalinsky

One option is to use regular expressions:

一种选择是使用正则表达式:

if (str.match("^Hello")) {
   // do this if begins with Hello
}

if (str.match("World$")) {
   // do this if ends in world
}

回答by sje397

For startswith, you can use indexOf:

对于startswith,您可以使用indexOf:

if(str.indexOf('Hello') == 0) {

...

...

ref

参考

and you can do the maths based on string length to determine 'endswith'.

并且您可以根据字符串长度进行数学运算以确定“endswith”。

if(str.lastIndexOf('Hello') == str.length - 'Hello'.length) {

回答by Sebastien P.

There is no need of jQuery to do that. You could code a jQuery wrapper but it would be useless so you should better use

不需要 jQuery 来做到这一点。你可以编写一个 jQuery 包装器,但它没有用,所以你应该更好地使用

var str = "Hello World";

window.alert("Starts with Hello ? " + /^Hello/i.test(str));        

window.alert("Ends with Hello ? " + /Hello$/i.test(str));

as the match() method is deprecated.

因为 match() 方法已被弃用。

PS : the "i" flag in RegExp is optional and stands for case insensitive (so it will also return true for "hello", "hEllo", etc.).

PS:RegExp 中的“i”标志是可选的,代表不区分大小写(因此它也会为“hello”、“hEllo”等返回true)。

回答by Salvador Dali

You do not really need jQuery for such tasks. In the ES6 specification they already have out of the box methods startsWithand endsWith.

对于此类任务,您实际上并不需要 jQuery。在 ES6 规范中,他们已经有了开箱即用的方法startsWithendsWith

var str = "To be, or not to be, that is the question.";
alert(str.startsWith("To be"));         // true
alert(str.startsWith("not to be"));     // false
alert(str.startsWith("not to be", 10)); // true

var str = "To be, or not to be, that is the question.";
alert( str.endsWith("question.") );  // true
alert( str.endsWith("to be") );      // false
alert( str.endsWith("to be", 19) );  // true

Currently available in FF and Chrome. For old browsers you can use their polyfills or substr

目前在 FF 和 Chrome 中可用。对于旧浏览器,您可以使用它们的 polyfills 或 substr

回答by Mr. Pumpkin

You can always extend Stringprototype like this:

你总是可以String像这样扩展原型:

//  Checks that string starts with the specific string
if (typeof String.prototype.startsWith != 'function') {
    String.prototype.startsWith = function (str) {
        return this.slice(0, str.length) == str;
    };
}

//  Checks that string ends with the specific string...
if (typeof String.prototype.endsWith != 'function') {
    String.prototype.endsWith = function (str) {
        return this.slice(-str.length) == str;
    };
}

And use it like this:

并像这样使用它:

var str = 'Hello World';

if( str.startsWith('Hello') ) {
   // your string starts with 'Hello'
}

if( str.endsWith('World') ) {
   // your string ends with 'World'
}

回答by 16kb

ES6 now supports the startsWith()and endsWith()method for checking beginning and ending of strings. If you want to support pre-es6 engines, you might want to consider adding one of the suggested methods to the Stringprototype.

ES6 现在支持startsWith()andendsWith()方法来检查strings 的开头和结尾。如果您想支持 es6 之前的引擎,您可能需要考虑将建议的方法之一添加到String原型中。

if (typeof String.prototype.startsWith != 'function') {
  String.prototype.startsWith = function (str) {
    return this.match(new RegExp("^" + str));
  };
}

if (typeof String.prototype.endsWith != 'function') {
  String.prototype.endsWith = function (str) {
    return this.match(new RegExp(str + "$"));
  };
}

var str = "foobar is not barfoo";
console.log(str.startsWith("foob"); // true
console.log(str.endsWith("rfoo");   // true