1

I have song titles that I am placing - ANY song that has an apostrophe breaks the the code - For example, when I try to ad "That's Alright", it registers as "That" because the ' breaks. Here is the isolated line of code. Can someone recommend a fix that will replace the ' with \' ?

"<input type='hidden' name='title' value='"+val.sources[0].title+"'>"+

tried a variation of code from W3 Schools

var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "W3Schools");

But I believe the syntax was off...

"<input type='hidden' name='title' value='"+val.sources[0].title+"'>"+

I want the hidden field value to not break at the '

<?php if($playlist!='fav' && $membership == '2'){ // REMOVED $list ?>
            "<form id='addFavorite' action='process_fav_add_pro_preview.php?p=<?php echo $playlist; ?>' method='post'><input type='hidden' name='favid' value='"+val.sources[0].track_id+"'>"
            +
            "<input type='hidden' name='mediatype' value='"+val.sources[0].media+"'>"+
            "<input type='hidden' name='title' value='"+val.sources[0].title+"'>"+
            "<input type='hidden' name='artist' value='"+val.sources[0].artist+"'>"+
            "<input type='hidden' name='source_url' value='"+val.sources[0].src+"'>"+
            "<input type='hidden' name='credits' value='"+val.sources[0].credits+"'>"+
            "<input type='hidden' name='user' value='<?php echo $id; ?>'>"+
            "<input type='hidden' name='playlists' value='<?php echo $playlist; ?>'>"+
            "<button class='playlist-favorite' id='sub'><i class='fa fa-heart faa-flash animated-hover' style='color:#ff4444'></i></button>"+
            "</form>"
            +
            <? } ?>
majidarif
  • 18,694
  • 16
  • 88
  • 133
Davo
  • 181
  • 1
  • 11
  • 1
    Can you please put your code with more details? – RN92 Feb 06 '19 at 04:55
  • Some more JS code would help understand. The first line makes little sense. Is it a part of a JS variable? – Sid Feb 06 '19 at 04:57
  • Possible duplicate of [Escaping Strings in JavaScript](https://stackoverflow.com/questions/770523/escaping-strings-in-javascript) – Nick Parsons Feb 06 '19 at 04:59
  • Why are you putting single inverted commas and then double inverted commas? double inverted commas only should suffice `value="+val.sources[0].title+"`. I think the single inverted commas you put is breaking the value after apostrophe – Aaditya Thakkar Feb 06 '19 at 04:59
  • I am mixing because I am combining it with html and they are requiring the opposite marks? – Davo Feb 06 '19 at 05:19

1 Answers1

0

To accept the string that has apostrophe or single quote in it, you can use the following method:

function escapeRegExp(text) {
  return text.replace(/[']/g, '\\$&');
}

This is a variation of the following method:

function escapeRegExp(text) {
  return text.replace(/[-[\]{}()*+?.,'\\^$|#]/g, '\\$&');
}

Referenced from this question at Stack Overflow.

If you pass the value as escapeRegExp("That's alright"), then it becomes That\'s alright

UPDATE: Here is what you can try to do:

$("input[name='title']").each(function(){
 var text = $(this).val();
  $(this).val(escapeRegExp(text));
});

function escapeRegExp(text) {
  return text.replace(/[']/g, '\\$&');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type='text' name='title' value="That's alright">

Here I have just changed the type to text so that the output is readable, but if you change it back to hidden again, the code will work.

Code_Ninja
  • 1,729
  • 1
  • 14
  • 38
  • I edited the post and added the entire code block... I understand what you have posted but how do I implement it within the context of my code block? – Davo Feb 06 '19 at 05:17
  • @Davo Can you please point to the field, where you are adding `That's alright`, because your naming conventions are a bit confusing with the context of the question. – Code_Ninja Feb 06 '19 at 05:20
  • +val.sources[0].title+ – Davo Feb 06 '19 at 05:25
  • Are you suggesting: ""+ – Davo Feb 06 '19 at 05:26
  • @Davo Yes, let me try to explain it by editing my answer – Code_Ninja Feb 06 '19 at 05:27
  • For the record, I tried that and it "breaks the code" - I am sure it is my syntax – Davo Feb 06 '19 at 05:28
  • @Davo, there is something wrong with the php tags in your code, please check that. – Code_Ninja Feb 06 '19 at 05:32
  • I see how it works but I have placed it above, below and inside my code block - and it doesmt work as yours - I am sure it about the exact placement - My result upon submissions shows "escapeRegExp(That" – Davo Feb 06 '19 at 05:39
  • Can I not place it directly in the line of code? I know php allows you to place it directly in a single line, such as: $title = str_replace("'","\'",$title); --- does JS allow such? – Davo Feb 06 '19 at 05:46