ある文字列に別の文字列が含まれているかどうかをJavaScriptで確認するにはどうすればよいですか? 私は次のようなものを持っています:
var a = "Paris, France"
var b = "Paris.."
a.match(b) //returns "Paris, " but should return null
問題は、一致が正規表現を使用していることだと思います。Sympols
.,-/\
などを許可する可能性はありますか? ありがとう
ある文字列に別の文字列が含まれているかどうかをJavaScriptで確認するにはどうすればよいですか? 私は次のようなものを持っています:
var a = "Paris, France"
var b = "Paris.."
a.match(b) //returns "Paris, " but should return null
問題は、一致が正規表現を使用していることだと思います。Sympols
.,-/\
などを許可する可能性はありますか? ありがとう
ある文字列に別の文字列が含まれているかどうかを確認するには、次を使用しますString.indexOf()
。
var str = 'Paris, France';
var strIndex = str.indexOf('Paris..');
if(strIndex == -1) {
//string not found
} else {
//string found
}
ただし、contains()
関数が必要な場合に備えて、次のように追加できString
ます。
if(!('contains' in String.prototype)) {
String.prototype.contains = function(str, startIndex) {
return -1 !== String.prototype.indexOf.call(this, str, startIndex);
};
}
var str = 'Paris, France';
var valid = str.contains('Paris..');
if(valid) {
//string found
} else {
//string not found
}