Skip to main content

String Replace function in IE and Firefox

Last week I was struggling with string replace function.The requirement was strange but its not in scope of this post. After spending one whole day and achieving the requirement, I have decided to club all the possible usage of replace funcation.

lets start with the simplest form of Replace function.

  var str = "The world is beautiful, just you need beautiful heart";
        str = str.replace('beautiful', 'lovely');

In above replace call, the first only occurrence of beautiful will be replaced by heart. The resultant string will be "The world is lovely, just you beautiful heart."

The Replace() function has two arguments
  1. string or regex to be replaced
  2. string or function[that return string] to be in placed.   

 we'll look in every possible scenarios.

Now if I want to make my replace function case insensitive, I do have to make little bit modification.
as follows
    var str = "The world is Beautiful, just you need Beautiful heart";
          str = str.replace(/beautiful/i, 'lovely');

In above case the  the first occurrence of Beautiful will be replaced by lovely.


Now I want to replace all the occurrence of the Beautiful irrespective of case. then a small modification is needed as follows.

  var str = "The world is Beautiful, just you need beautiful heart";
          str = str.replace(/beautiful/gi, 'lovely');

here all the occurrence of Beautiful/beautiful will be replaced by lovely word.

Now I want to give to be replaced string dynamically it can be achieved as follows.


 
 var str = "The world is Beautiful, just you need beautiful heart";
     str = str.replace(/beautiful/gi, strTo());

As we can see here the second parameter of replace function is a call to function which will return some formatted string.
Now in case where the string which is to be replaced is dynamically supplied then we have
to take another approach as follows
 
        var strTarget='beautiful'
        var str = "The world is Beautiful, just you need beautiful heart";
        str = str.replace(strTarget, strTo(),'gi'); 
however this function will only work with firefox, to make it work in IE demands some work around as follows.
 
   var strTarget = new RegExp('beautiful', 'gi');
        var str = "The world is Beautiful, just you need beautiful heart";
        str = str.replace(strTarget, strTo()); 
here we have to create our own regexp that will be supplied to replace function as first parameter, and it works great with IE too...

Comments

Popular posts from this blog

Accessing Enum in template–Angular with Typescript

By reading title of  this post, It  seems some what misleading. In Angular 2 (or higher version for that matter). We cannot directly access  Enum or any other type for that matter into template. so, for an example  consider following example now if we try to access Enum WeekDays in HTML template as follows It would be resulted in runtime error shown below To resolve this error we should be assigning enum to component property as shown below. Reason behind this is , Component is the execution context for an Angular application. We can reference only those properties in template which are defined in component or have scope in component.

Setting up Visual Studio code for Java Spring boot development

  Download Visual Studio Code to your machine by clicking here . You will be redirected to official download site for VS Code. You can download VS depending on your OS. When we open VS Code and click on explorer (Ctrl+Shft+E). There two options available by default. Open Folder Clone Repository Visual Studio code is an Editor not a full fledged IDE. But we can configure VS Code by installing relevant extensions available at  https://marketplace.visualstudio.com/vscode . To install extension directly from Visual Studio, click on Extension icon on Menu item at left panel. Alternatively we can use Ctrl+Shft+X keyboard short cut. First extension we going to search is "Extension Pack From Java" from Microsoft. This extension pack is collection of extensions which is needed to enabled Java development in General. Language Support for Java - Rad Hat Debugger for Java - Microsoft Maven for Java - Microsoft Test Runner for Java - Microsoft Project Manager for Java - Microsoft. Next ex

Visual Studio code - Setting up Tomcat server

  This is the second post in series of Setting up Visual Studio code for Java Spring Boot development. First post of this series can be found here . In this article we will see how we can configure VS Code to run Apache Tomcat server via extension. So lets begin. Open your VS Code and click on Extensions menu item. Now search for "Tomcat For Java". Select extension From Wei Shen and click install. Once this extension is installed. we can see new tab panel enabled in Explorer window. When we expand it, its empty. Now we need to configure Tomcat. For that, we need to download Apache Tomcat from official site. To download Tomcat click here to redirect to official site. There are multiple version available to download. For this demo we will download Tomcat Version 9.  Once Tomcat is downloaded to our machine. We now navigate back to VS code. We can now configure Tomcat by click on "+" sign on "Tomcat Servers" panel. When we click on Add Tomcat Server "