1

I want to get some textarea text and replace all bullet point html entities • with ·.

The usual approach str.replace(/•/g,"·"); doesn't work.

Any advice would be appreciated.

Kiran
  • 20,167
  • 11
  • 67
  • 99
damo_inc
  • 653
  • 1
  • 9
  • 26

2 Answers2

3

When you're getting the text value back from the textarea, it has already been converted to its actual character. To do a string replacement on that string, either

  1. convert all characters to their html entity counterparts, then proceed with what you're doing or
  2. use the character in the regex directly.

Here's an example of the second approach.

var newText = oldText.replace(/•/g, "");

You can fiddle with an example here.

If you want to go with the first approach, see this question and its answers for ways to convert characters in a piece of text to their corresponding html entities.

Community
  • 1
  • 1
Anurag
  • 140,337
  • 36
  • 221
  • 257
  • 1
    The code presented removes the bullet points, and the jsfiddle code replaces them with the text “[DELETED]” but the principle is of course clear. For the operation in the question, the replacing string would be `"·"` or, equivalently, `"·"`. If the intent is to replace a character string like `•` if entered literally by the user, then it’s a different question. – Jukka K. Korpela Jan 18 '12 at 12:45
2

If you want to do this without jQuery:

var myTextarea = document.getElementById('id_of_your_textarea');
myTextarea.value = myTextarea.value.replace(/•/g, '·');

jQuery:

$("#myTextarea").val( $("#myTextarea").val().replace(/•/g, '·') );

.val() will get the value from an input element, .val('str') will set a value.

RickN
  • 12,537
  • 4
  • 24
  • 28