Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 16, 2023 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/5960ddfa51dea0d168e1cb3ef036aacc to your computer and use it in GitHub Desktop.
Save code-boxx/5960ddfa51dea0d168e1cb3ef036aacc to your computer and use it in GitHub Desktop.
Javascript Case Insensitive String Compare

JAVASCRIPT CASE INSENSITIVE STRING COMPARE

https://code-boxx.com/case-insensitive-string-comparison-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// (A) STRINGS
var first = "Hello World";
var second = "hello world";
// (B) COMPARISON
console.log(first == second); // false
console.log(first.toLowerCase() == second.toLowerCase()); // true
console.log(first.toUpperCase() == second.toUpperCase()); // true
// (A) VARIABLES
var first, second;
// (B) LOCALE COMPARE
// (B1) RETURNS 0 WHEN BOTH FIRST & SECOND ARE EQUAL
first = "C"; second = "C";
console.log(first.localeCompare(second)); // 0 (equal)
// (B2) RETURNS 1 WHEN FIRST IS AFTER SECOND
first = "C"; second = "B";
console.log(first.localeCompare(second)); // 1 (c is after b)
// (B3) RETURNS -1 WHEN FIRST IS BEFORE SECOND
first = "A"; second = "B";
console.log(first.localeCompare(second)); // -1 (a is before b)
// (C) CAN ALSO COMPARE "COMPLEX STRINGS"
first = "Car"; second = "Cow";
console.log(first.localeCompare(second)); // -1 (car is before cow)
console.log(second.localeCompare(first)); // 1 (cow is after car)
// (A) STRINGS
var first = "CAR";
var second = "car";
// (B) DEFAULT LOCALE COMPARE
// This will most likely return 1.
// Capital "CAR" has precedence over small "car".
console.log(first.localeCompare(second));
// (C) LOCALE COMPARE WITH OPTIONS
// Set to case-insensitive compare.
// This will now return 0.
console.log(
first.localeCompare(second, "en", {
sensitivity: "base",
ignorePunctuation: true
})
);
// (A) THE SETING
var thestring = "COW";
// (B) CASE SENSITIVE PATTERN MATCHING
var pattern = /^cow$/; // must match "cow" exactly
console.log(pattern.test(thestring)); // false
// (C) CASE INSENSITIVE PATTERN MATCHING
var pattern = /^cow$/i; // match "cow"
console.log(pattern.test(thestring)); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment