Remove and replace Plus (+) character with space using RegularExpression (Regex) and JavaScript

Last Reply one year ago By Shashikant

Posted one year ago

 

<script type="text/javascript">
        $(document).ready(function () {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "/Default.aspx/Spotlight",
                data: '{}',
                dataType: "json",
                success: function (data) {
                    var myobject = JSON.stringify(data.d);

                    $($.parseJSON(myobject)).each(function (index, el) {

                        $('.titleofpost').text(el.post_title).replace('/+/g', ' ');
                        $('.titleofpost').attr('href', el.post_title);
                        $('.appendspotlight').text(el.post_description);

                    //   document.write(el.post_title,el.post_date, el.post_credit, el.post_description, el.post_views);
                   });
                },
                error: OnError
            });
        });
        
        }
    </script>

 

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    [WebMethod]
    public static List<Posts> Spotlight()
    {
        string query = "select TOP 1 post_date,post_tags,post_title,post_credit,post_id,post_description,post_views,post_image,post_know from posts where post_highlited=1";
        SqlCommand cmd = new SqlCommand(query);
        DataSet ds = GetData(cmd);
        DataTable dt = ds.Tables[0];

        foreach (DataRow item in ds.Tables[0].Rows)
        {
            Posts post = new Posts();
            post.post_title = HttpUtility.UrlEncode(item["post_title"].ToString());
            post.post_credit = item["post_credit"].ToString();
            post.post_date = String.Format("{0:ddd, MMM d, yyyy}", item["post_date"]);
            post.post_description = item["post_description"].ToString();
            post.post_know = item["post_know"].ToString();
            post.post_views = Convert.ToInt16(item["post_views"]);
            productt.Add(post);
        }
        return productt;
    }

I am getting json value for post_title in this format

since i am using httputility.urlencode

Why+every+One+Should+Blog

I want to remove + and make it look like 

Why every One Should Blog

 

Posted one year ago

Please refer below sample.

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function Remove() {
            var value = document.getElementById('txt1').value;
            value = value.replace(/[+]/g, ' ');
            alert(value);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Enter Text :
        <input id="txt1" type="text" value=" " />
        <input type="button" value="Get" onclick="Remove()" />
    </div>
    </form>
</body>
</html>

Demo

I hope this will help you out.

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html?aff_id=108