2

I am trying to append a parsed email to a textarea with javascript, and this is proving to be particularly difficult because of the < & > in email addresses like <[email protected]>

Here is an example of my situation in action. https://jsfiddle.net/xxchz97L/

So I am trying to do a str.replace on the < & > but nothing I do seems to work. Does anyone know how to do this?

Here is a simple excerpt of my code. I am also including jQuery.

HTML

<textarea class="form-control template_data" id="content" name="content" rows="8" cols="80"></textarea>

Javascript

var my_text = "From: Foo Bar <[email protected]> Date: Sat, Apr 8, 2017 at 2:29 PM";
var regEx = '/<|>/g';
my_text.replace(regEx, "*");
my_text = my_text.replace("&lt;", "*");
my_text = my_text.replace("&gt;", "*");
$('#content').append(my_text);
alert(my_text);

PS

I figured there would be no way to append < | > into a textarea as html would think I was posting HTML. If there is someone that does know how to do this please let me know.

4
  • Why not my_text.replace("<", "*").replace(">", "*"); ? Commented May 8, 2017 at 18:53
  • regex = /[<>]/g Commented May 8, 2017 at 18:53
  • if i use following in your fiddle, it works: my_text = my_text.replace("<", "*"); my_text = my_text.replace(">", "*"); Use < > instead of codes Commented May 8, 2017 at 18:54
  • You can include those characters in the <textarea> if you turn them into HTML entities (convert < to &lt; and so on). You don't have to turn them all into asterisks. Commented May 8, 2017 at 19:22

4 Answers 4

2

Use the RegExp, is very easy :)

var regEx = new RegExp("[<>]","g");

for replace use:

yourString = yourString.replace(regEx, "yourReplace");

Do not forget the immutability of the string

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

5 Comments

So you cleared all the line breaks then ran it. That seems like a pretty clean solution.
Not only that, but had a detail in the first replace, which was not reassigning to the String. You will not even need the other two "replace"
@ThomasValadez why do you want to change the characters to "*"? Why not just HTML-escape them?
@Pointy I was experimenting with a few things, I figured that would be the easiest option. Are you talking about something like this stackoverflow.com/questions/6234773/…
I'm talking about something like my answer :)
2

A general HTML sanitizer function really only needs one .replace() call:

var sanitize = function() {
  var map = { "<": "&lt;", ">": "&gt;", "&": "&amp;" },
      rx = /[<&>]/g;

  return function(text) {
    return text.replace(rx, function(match) {
      return map[match];
    };
  };
}();

The .replace() callback takes each matched special character and uses it as a key to lookup the replacement in a map.

With this, you can preserve the actual content for the <textarea> if you use the function on the contents when the page is prepared.

Note that you don't have to worry about this when setting the .value property of the <textarea> with JavaScript.

3 Comments

sorry to be asking a dumb question , but on what does this function get called on and who is passing text into the first inner function ?
@AlexanderSolonik The function to call is sanitize(). If you were to call sanitize("<b>Hello World</b>") the return value would be the string "&lt;b&gt;Hello World&lt;/b&gt;"
perfect ! got it :)
0

You are trying to replace the escaped characters when no such characters exist in the string.

Change:

my_text = my_text.replace("&lt;", "*");
my_text = my_text.replace("&gt;", "*");

to:

my_text = my_text.replace("<", "*");
my_text = my_text.replace(">", "*");

1 Comment

Awesome that worked for the first line. Now I have two questions. Why doesn't the regex expression take care of that? and how would I make it span multiple lines of text?
0

Your Problem seems to be an incorrect RegEx. Try the following:

my_text = my_text.replace(/(\<|\>)/g, '');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.