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.

3
  • everything works. Show jsFiddle with your problem, and please tell us - what browser is having this issue ? Commented Jan 18, 2012 at 9:20
  • Are this bullet points encoded like this or they come from list elements? Commented Jan 18, 2012 at 9:21
  • 1
    Works just fine: jsfiddle.net/yXGD4/1 Commented Jan 18, 2012 at 9:23

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.