Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,449 answers

573 users

How to remove duplicate case‑insensitive words separated by multiple delimiters from a string in JavaScript

1 Answer

0 votes
/*
    Remove duplicate case‑insensitive words separated by multiple delimiters.

    Features:
    - Case‑insensitive comparison (toLowerCase)
    - Preserves original casing of the first occurrence
    - Trims whitespace around tokens
    - Supports ANY number of delimiters (including multi‑character)
    - Preserves original order
    - Efficient O(n) lookup using Set
*/

/* ---------------------------------------------------------------
   Build a regex that matches ANY delimiter
--------------------------------------------------------------- */
function buildDelimiterRegex(delimiters) {
    // Escape delimiters so characters like "|" or "*" are treated literally
    const escaped = delimiters.map(d => d.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
    
    return new RegExp(`(?:${escaped.join("|")})+`, "g");
}

/* ---------------------------------------------------------------
   Split input string using multiple delimiters
--------------------------------------------------------------- */
function splitByDelimiters(input, delimiters) {
    const regex = buildDelimiterRegex(delimiters);
    
    return input.split(regex).map(t => t.trim()).filter(t => t.length > 0);
}

/* ---------------------------------------------------------------
   Remove duplicates (case‑insensitive)
--------------------------------------------------------------- */
function removeDuplicatesCaseInsensitive(tokens) {
    const seen = new Set();
    const unique = [];

    for (const token of tokens) {
        const key = token.toLowerCase();
        if (!seen.has(key)) {
            seen.add(key);
            unique.push(token);
        }
    }
    
    return unique;
}

/* ---------------------------------------------------------------
   Join tokens with a chosen delimiter
--------------------------------------------------------------- */
function joinTokens(tokens, delimiter) {
    return tokens.join(delimiter);
}

/* ---------------------------------------------------------------
   Remove duplicates (Multi Delimiter case‑insensitive)
--------------------------------------------------------------- */
function removeDuplicatesMultiDelimiterCI(input, delimiters, outputDelimiter) {
    const tokens = splitByDelimiters(input, delimiters);
    const unique = removeDuplicatesCaseInsensitive(tokens);
    
    return joinTokens(unique, outputDelimiter);
}

/* ---------------------------------------------------------------
   Main
--------------------------------------------------------------- */

const s = "AAA | aaa ,   aAA * aaA | AAa | AAA   | BBB | ccc ---- CCC | AAA ; aaa | bbb";

const delimiters = ["  ", "|", ",", "*", "-", ";"];

const result = removeDuplicatesMultiDelimiterCI(s, delimiters, " | ");

console.log(result);



/*
run:

AAA | BBB | ccc

*/

 



answered Aug 2 by avibootz

Related questions

...