localeCompare【文字列比較 (ロケール)】
startsWith【開始文字列判定】
endsWith【終了文字列判定】

String.prototype.localeCompare【文字列比較 (ロケール)】

メモ

  • システムのデフォルト ロケールによるソート順で文字列比較
    • 通常の比較は、比較 演算子 (< 【小なり 演算子】等)を使用
  • 外部リンク (英語)
    String.prototype.localeCompare (that)
    String.prototype.localeCompare ( that [, reserved1 [ , reserved2 ] ] )
    ES2022 (13) ES2021 (12) ES2020 (11)

構文

string.localeCompare( that ) 
string.localeCompare( that[, reserved1[, reserved2]] ) 

比較結果 (詳細は下記参照)
that比較文字列
reserved1実装依存
reserved2実装依存
比較結果説明
比較元(this)が前
0一致
比較元(this)が後

console.log("ab".localeCompare("aa"));      // 出力:1
console.log("ab".localeCompare("ab"));      // 出力:0
console.log("ab".localeCompare("ac"));      // 出力:-1
console.log("ä".localeCompare("z", "de"));  // 出力:-1 (ドイツ語:負数)
console.log("ä".localeCompare("z", "sv"));  // 出力:1  (スウェーデン語:正数)

String.prototype.startsWith【開始文字列判定】

メモ

構文

string.startsWith( searchString[, position] )

判定結果
true:判定文字列で開始
false:その他
searchString判定文字列
position文字列開始位置 (0~ 詳細は下記参照)
例外TypeError 例外 (判定文字列が正規表現)
(RegExp[ @@match ]【正規表現判定】プロパティ  をfalseに設定すれば回避可能)
position説明
省略0 (先頭)
< 00 (先頭)
文字列長 <文字列長

var str = "abcde";
console.log(str.startsWith("bc"));    // 出力:false
console.log(str.startsWith("bc", 1)); // 出力:true

var regEx = /bc/;
regEx[Symbol.match] = false;          // RegExp[@@match]
console.log(str.startsWith(regEx));   // 出力:false
console.log(str.startsWith(/bc/));    // TypeError 例外

String.prototype.endsWith【終了文字列判定】

メモ

構文

string.endsWith( searchString[, endPosition] )

判定結果
true:判定文字列で終了
false:その他
searchString判定文字列
endPosition文字列終了位置 (この位置は含まない:詳細は下記参照)
例外TypeError 例外 (判定文字列が正規表現)
(RegExp[ @@match ]【正規表現判定】プロパティ  をfalseに設定すれば回避可能)
endPosition説明
省略文字列長
< 00 (先頭)
文字列長 <文字列長

var str = "abcde";
console.log(str.endsWith("cd"));    // 出力:false
console.log(str.endsWith("cd", 4)); // 出力:true

var regEx = /cd/;
regEx[Symbol.match] = false;        // RegExp[@@match]
console.log(str.endsWith(regEx));   // 出力:false
console.log(str.endsWith(/cd/));    // TypeError 例外