Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    39
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Using javascript string REPLACE method with "ignore case flag"?

    I currently use the string method REPLACE to bold all instances of a relevant term in a string with a function similar to below

    function boldRelevant(text, relevantTerm) {
    return text.replace(relevantTerm, "<b>" + relevantTerm + "</b>", "gi");
    }

    Is it possible to have the case of the relevant term not matter when linearly searching for its instances in text and still have the replacement string to have the same case as the string that was found?

    PHP Code:
    1. For example currently the function does this:
    var 
    text "HELLO THIS IS IN CAPS"relevantTerm "Hello";
    return 
    statement "<b>Hello</b> THIS IS IN CAPS"
    2. var text "hello this is in caps"relevantTerm "cApS";
    return 
    statement "hello this is in <b>cApS</b>" 
    Is it possible to make it the function return below instead?
    PHP Code:
    1. return statement "<b>HELLO</b> THIS IS IN CAPS"
    2. return statement "hello this is in <b>caps</b>" 
    Thanks for the help

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    You mean like this?
    Code:
    function boldify(m){
        return '<b>'+m+'<\/b>';
    }
    
    var
        text1='HELLO THIS IS IN CAPS',
        text2='hello this is in caps',
        pattern1=/hello/gi,
        pattern2=/CaPs/gi,
        res1=text1.replace(pattern1,boldify), // => '<b>HELLO</b> THIS IS IN CAPS'
        res2=text2.replace(pattern2,boldify); // => 'hello this is in <b>caps</b>'
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New Coder
    Join Date
    Jul 2006
    Posts
    39
    Thanks
    7
    Thanked 0 Times in 0 Posts
    thanks for the reply. how do i accomplish it with having the pattern1 and pattern2 being variables as opposed to constants?

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Code:
    function boldify(m){
        return '<b>'+m+'<\/b>';
    }
    
    function stringToRegExp(pattern, flags){
        return new RegExp(
            pattern.replace(/[\[\]\\{}()+*?.$^|]/g, function(m){return '\\'+m;}),
            flags);
    }
    
    var
        text1='HELLO THIS IS IN CAPS',
        text2='hello this is in caps',
        term1='hello',
        term2='CaPs',
        pattern1=stringToRegExp(term1, 'gi'),
        pattern2=stringToRegExp(term2, 'gi'),
        res1=text1.replace(pattern1, boldify), // => '<b>HELLO</b> THIS IS IN CAPS'
        res2=text2.replace(pattern2, boldify); // => 'hello this is in <b>caps</b>'
    Last edited by liorean; 02-23-2008 at 02:25 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • Users who have thanked liorean for this post:

    harjanto (02-23-2008)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •