0

my name is Ahmed and I have a page that contains a textarea and a button to send data in the textarea via Ajax after clicking but I want to replace some strings before sending and the strings are  , ­, &, <

and more in the JS code

$('#CT').click(function(){
   var textarea=$('#ed_level_2');
   textarea.val(textarea.val().replace(/&/g,"").replace(/</g,"").replace(/>/g,"").replace(/"/g,"").replace(/–/g,"").replace(/—/g,"").replace(/ /g,"").replace(/ /g,"").replace(/ /g,"").replace(/­/g,"").replace(/©/g,"").replace(/™/g,"").replace(/®/g,""));
});

I am looked at some sites and I can not find a way to replace more than two strings Such as the code at the bottom

finalurl.replace(/and/g, '&').replace(/eq/g, '=');
6
  • replace is a string method in this case, which is vanilla javascript Commented May 14, 2019 at 16:30
  • What you have should work fine, although is a little long winded. Have you checked to see that the HTML entities are actually shown like that within the val() you retrieve? Commented May 14, 2019 at 16:32
  • This would work, although if you're replacing them all with an empty string, a regex would be easier. Better yet, use a library explicitly designed to handle HTML entity replacement... and keep in mind that depending on what you're actually trying to it might be better to replace them with reasonable characters, like quotes, dashes, etc. Commented May 14, 2019 at 16:33
  • My code does not replace all words before sending Commented May 14, 2019 at 16:34
  • You want to replace a string having multiple occurrences? Commented May 14, 2019 at 16:36

3 Answers 3

1

You can replace multiple strings at once by separating them with a vertical pipe, so this:

$('#CT').click(function(){
   var textarea=$('#ed_level_2');
   textarea.val(textarea.val().replace(/&/g,"").replace(/</g,"").replace(/>/g,"").replace(/"/g,"").replace(/–/g,"").replace(/—/g,"").replace(/ /g,"").replace(/ /g,"").replace(/ /g,"").replace(/­/g,"").replace(/©/g,"").replace(/™/g,"").replace(/®/g,""));
});

Could be written as:

$('#CT').click(function(){
   var textarea=$('#ed_level_2');
   textarea.val(textarea.val().replace(/&|<|>|"|–|—| | | |­|©|™|®/g,""));
});

I'm not sure what sites you looked at, but w3schools has plenty of examples of different javascript string functions, including replace():

https://www.w3schools.com/jsref/jsref_replace.asp

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

Comments

1

As I understand, you want to replace (remove in your case) multiple instant at once. Try this

textarea.val(textarea.val().replace(/\&(amp|lt|gt|quot|ndash|mdash)\;/gi,"")

better build a list

const myList = ['amp', 'lt', 'gt', 'quot', 'ndash'];
const regex = new RegExp('\&' + myList.join('|') + '\;', 'gi');
textarea.val(textarea.val().replace(regex, ''));

if you need to replace all html entity then just use regex

textarea.val(textarea.val().replace(/\&[a-z]+\;/gi, ''));

Comments

0

It depends on what problem you're trying to solve as to wether you want to replace specific entities, strip all html, or both.

$(function() {

  function replaceEntities(badString) {
    return badString.replace(/&|<|&gt|"|–|—| | | |­|©|™|&reg/gi, "");
  };

  function stripHtml(badString) {
    return $($.parseHTML(badString)).text();
  }

  $("form").on("submit", function(e) {
    e.preventDefault();
    var inval = $("#in").val();
    $("#out1").val(replaceEntities(inval));
    $("#out2").val(stripHtml(inval));
  })


})
label {
  display: inline-block;
  margin: 5px;
  padding: 5px;
  border: 1px dotted #ccc;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  <label>Input
  <textarea id="in" placeholder="try some junk html in here"></textarea>
  </label>
  <label>replaceEntities()
  <textarea id="out1" readonly=readonly></textarea>
  </label>
  <label>stripHTML()
  <textarea id="out2" readonly=readonly></textarea>
  </label>
  <input type="submit" />
</form>

1 Comment

@Nopel I'm not suggesting that you use this exact code. Its an example. There's nothing in it that prevents you from using Ajax or adding more lines to the function.

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.