Click here to Skip to main content
12,688,944 members (32,257 online)
Click here to Skip to main content
Add your own
alternative version

Stats

17.7K views
14 bookmarked
Posted

Javascript Replace All with Case Insensitive Matching

, 25 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Custom Javascript replaceAll() using prototype with Case Insensitive Matching!

Introduction

I was using the regex replace all, but found out it had errors replacing certain reserved characters without escaping them first. So I wrote a custom method, it seems to work pretty well. Please, help test with internet explorer and windows. Also, please let me know if I have goofed with string pointer positions. I haven't fully tested it yet, but I hope it is flawless. Thanks!

Please speed test it here.
http://jsperf.com/javascript-replace-all/8[^]
http://jsperf.com/javascript-replace-all/10[^]

JavaScript - Prototype Version

// Find, Replace, Case
String.prototype.replaceAll = function(_f, _r, _c){ 

  var o = this.toString();
  var r = '';
  var s = o;
  var b = 0;
  var e = -1;
  if(_c){ _f = _f.toLowerCase(); s = o.toLowerCase(); }

  while((e=s.indexOf(_f)) > -1)
  {
    r += o.substring(b, b+e) + _r;
    s = s.substring(e+_f.length, s.length);
    b += e+_f.length;
  }

  // Add Leftover
  if(s.length>0){ r+=o.substring(o.length-s.length, o.length); }

  // Return New String
  return r;
};
//
How to use:
// Replaces Case Matching - Param = FindString, ReplaceString, ICaseBoolean 
'Test to see if this works? (Yes|No)'.replaceAll('(Yes|No)', 'Yes!');

// Replaces Case Insensitive Matching with True
'Test to see if this works? (Yes|No)'.replaceAll('(yes|no)', 'Yes!', true);

JavaScript - Direct Function Version

function replaceAll(_s, _f, _r, _c){ 

  var o = _s.toString();
  var r = '';
  var s = o;
  var b = 0;
  var e = -1;
  if(_c){ _f = _f.toLowerCase(); s = o.toLowerCase(); }

  while((e=s.indexOf(_f)) > -1)
  {
    r += o.substring(b, b+e) + _r;
    s = s.substring(e+_f.length, s.length);
    b += e+_f.length;
  }

  // Add Leftover
  if(s.length>0){ r+=o.substring(o.length-s.length, o.length); }

  // Return New String
  return r;
}
How to use:
// Replaces Case Matching - Results = replaceAll(FindString, ReplaceString, ICaseBoolean) 
replaceAll('Test to see if this works? (Yes|No)', '(Yes|No)', 'Yes!');

// Replaces Case Insensitive Matching with True
replaceAll('Test to see if this works? (Yes|No)', '(yes|no)', 'Yes!', true);

PHP - Direct Function Version

function replaceAll($_s, $_f, $_r, $_c=false)
{ 
  $o = $_s;
  $r = '';
  $s = $o;
  $b = 0;
  $e = -1;
  if($_c){ $_f = strtolower($_f); $s = strtolower($o); }

  while(($e=strpos($s, $_f))!=false)
  {
    $r .= substr($o, $b, ($b + $e)) . $_r;
    $s = substr($s, ($e + strlen($_f)), strlen($s));
    $b += $e + strlen($_f);
  }

  // Add Leftover
  if(strlen($s)>0){ $r.=substr($o, (strlen($o) - strlen($s)), strlen($o)); }

  // Return New String
  return $r;
}
How to use:
// Replaces Case Matching - Results = replaceAll(FindString, ReplaceString, ICaseBoolean) 
replaceAll('Test to see if this works? (Yes|No)', '(Yes|No)', 'Yes!');

// Replaces Case Insensitive Matching with True
replaceAll('Test to see if this works? (Yes|No)', '(yes|no)', 'Yes!', true);

Javascript - Prototype REGEX Version

DO NOT USE! HAS ISSUES WITH CHARACTERS!
/* DO NOT USE - Regex has issues replacing reserved characters unless they are escaped. Found out the hard way! */
String.prototype.replaceAll = function()
{ 
  return this.toString().replace(new RegExp(arguments[0], 'g' +
 ((arguments[2])? 'i': '') ), arguments[1]); 
};

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

M@dHatter
Software Developer (Senior) Codevendor
United States United States
Please visit my personal website http://www.codevendor.com for my latest codes and updates.

Raise Bid

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
Anurag Gandhi27-Apr-14 6:21
professionalAnurag Gandhi27-Apr-14 6:21 
GeneralMy vote of 4 Pin
Nitij24-Apr-14 3:35
professionalNitij24-Apr-14 3:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170113.4 | Last Updated 25 May 2014
Article Copyright 2014 by M@dHatter
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid