JQuery to Replace a String with or without Regular Expression in Asp.Net C# VB.Net

String operation is one of the most common logical part in our development life. Replacing also a common functionality. In most cases we need to replace Character or String. Replacing can be a set of character or a regular expression. While replacing, we need to know how much occurrences we need to replace? Does the replace operation will be case sensitive? We will try to find out every answers from this post for dot net developers.

JQuery to Replace a String in Asp.Net Csharp VB.Net

HTML Markup:

    <asp:Label ID="Label1" runat="server" Text="Original String: money money MONEY 10 20" Font-Bold="true"></asp:Label><br />
    <hr />
    <asp:Label ID="Label2" runat="server" Text=""></asp:Label><br /><br />
    <asp:Label ID="Label3" runat="server" Text=""></asp:Label><br /><br />
    <asp:Label ID="Label4" runat="server" Text=""></asp:Label><br /><br />
    <asp:Label ID="Label5" runat="server" Text=""></asp:Label>

JQuery Code to Replace a String:

<script type="text/javascript">
    $(document).ready(function () {
        var income = "money money MONEY 10 20";
        $('#<%= Label2.ClientID %>').text("income.replace('money', 'Dollar'): " + income.replace('money', 'Dollar'));
        $('#<%= Label3.ClientID %>').text("income.replace(/money/g, 'Dollar'): " + income.replace(/money/g, 'Dollar'));
        $('#<%= Label4.ClientID %>').text("income.replace(/money/gi, 'Dollar'): " + income.replace(/money/gi, 'Dollar'));

        // Replace String using Regular Expression
        $('#<%= Label5.ClientID %>').text("income.replace(/\d+/gi, '$'): " + income.replace(/\d+/gi, '$'));

1. Normal JQuery Replace method replace only first occurrence.
2. Replace by ‘/g’ will replace every occurrences. But case sensitive.
3. Replace by ‘/gi’ will replace every occurrences and not case sensitive.

Hope now you can use JQuery string replace operation as per your requirement.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, Client, JQuery, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *