Removing characters with Jquery

Asked

Viewed 11,551 times

2

Is it possible to remove a specific character with jquery? I am wanting to remove the character • 'ball in character that is in the database of a website that I am reformulating. I mean, I have access to all the Htmls and Csss but the content. With that my list already has a specific Style but in the database has this bloody character • typed.

3 answers

5

Use a regular expression:

var mystring = "• este é um teste"
mystring.replace(/•/g , "");

You can pick up and change the content with $('seletor').html(). The advantage of using regular expression instead of replace() is that replace() only changes the first occurrence.

4


The does not have some basic rules that can be solved peacefully with javascript like data type conversions, data type identification, value rounding and string replacement.

So you can resolve this situation with javascript:

var str = "• Este é um texto de teste!•••";
var res = str.replace(/•/g, "");

document.getElementById("texto").innerHTML = res;
<span id="texto"></span>

To solve this same problem with jQuery would be as follows

var str = $("#texto").text();
var res = str.replace(/•/g, "");

$("#texto").text(res);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<span id="texto">• Este é um texto de teste!•••</span>

3

You can use the .split() to break the string and then re-join with the .Join():

"•bolinhas • bolinhas•".split('•').join(''); // dá "bolinhas  bolinhas"

Using the split/Join is faster than using regular expressions.

So you can do a function to clean strings:

function limparBolinhas(str){
    return str.split('•').join('');
}

and use in the code as required.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.