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 () {
                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);

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


[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    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"]);
        return productt;

I am getting json value for post_title in this format

since i am using httputility.urlencode


I want to remove + and make it look like 

Why every One Should Blog


Posted one year ago

Please refer below sample.


<html xmlns="">
<head runat="server">
    <script type="text/javascript">
        function Remove() {
            var value = document.getElementById('txt1').value;
            value = value.replace(/[+]/g, ' ');
    <form id="form1" runat="server">
        Enter Text :
        <input id="txt1" type="text" value=" " />
        <input type="button" value="Get" onclick="Remove()" />


I hope this will help you out.

I agree, here is the link: