文字列に存在する文字が別の文字列で3文字を連続して繰り返すかどうかを確認するには、正規表現パターンが必要です。例えば:
var string1 = "HelloWorld";
var string2 = "Work";
ここでは、string1の「Wor」という文字がstring2で繰り返されるため、trueを返す必要があります。
これに関する助け
使用split
、substring
およびincludes
var fn = function( string1, string2, matchChars ) {
return !!string2.split("").find( function(item, index){
if (index + matchChars <= string2.length )
{
return string1.includes( string2.substring( index, index + matchChars ) ); //check after each turn if the substring from index is included in string1 or not
}
return false;
});
}
console.log( fn("HelloWorld", "Work", 3) );
var fn = function(string1, string2, matchChars) {
return !!string2.split("").find(function(item, index) {
if (index + matchChars <= string2.length) {
return string1.includes(string2.substring(index, matchChars));
}
return false;
});
}
console.log(fn("HelloWorld", "Work", 3));
console.log(fn("HelloWorld", "Wod", 3));
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加