0

Hi I am trying to change the contents of a hidden input.

var temp = $("#header").val().replace("(/<\/div><div>/g","<br>");

I tried using the following replace but it does not change it.

May I know what I did wrong? I am trying to replace:

</div><div>

to

<br>

EDIT - this is my program flow

  • User enters input to a wysiwyg text editor which produces the output

  • Save the user input to database

  • Go to another page where the wysiwyg input is retrieved from database and save it to input type hidden

  • Replace the </div><div> to a <br> which is then outputted to a div

wirey00
  • 33,517
  • 7
  • 54
  • 65
RickyHalim
  • 295
  • 6
  • 22
  • What do you do with the `temp` variable after setting it? – nnnnnn Dec 11 '14 at 12:33
  • I want to output it to a pdf using jspdf, but it does not understand HTML codes, and I am planning to output the line separately after a split. – RickyHalim Dec 11 '14 at 12:34
  • what is an example of the contents you are trying to modify? and what is the expected result? You want to keep the first opening div and the last closing div? – wirey00 Dec 11 '14 at 12:34
  • 2
    So you are currently logging `temp` to the console or something and that's how you know the replace isn't working? Or...? – nnnnnn Dec 11 '14 at 12:35
  • I use alert to see the replaced content of temp – RickyHalim Dec 11 '14 at 12:36
  • 1
    http://jsfiddle.net/k9pxe3qg/ – Umesh Sehta Dec 11 '14 at 12:37
  • Though you really shouldn't [parse html with a regex](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags).. – wirey00 Dec 11 '14 at 12:47

2 Answers2

1

To replace all ocurrences in a string, the correct syntax with regExp is:

var temp = $("#header").val().replace(/<\/div><div>/g,"<br>");

You can test it, here:

'</div><div></div><div></div><div></div><div>'.replace(/<\/div><div>/g,"<br>");

returns:

<br><br><br><br>
Javier del Saz
  • 846
  • 1
  • 9
  • 16
  • It should be noted that this only replaces the first occurence of `
    `, you'll have to use Gaurang Tandon's solution to replace them all.
    – Drown Dec 11 '14 at 12:34
  • Yes as Drown said, it only replaces the first occurence, and I need to replace all – RickyHalim Dec 11 '14 at 12:35
  • Thank you Javier! I suppose it didn't work because I put a " on the Regexp – RickyHalim Dec 11 '14 at 12:43
  • you are welcome :), as you see if you quoted the regExp y treated as normal string – Javier del Saz Dec 11 '14 at 12:49
  • $(function(){ var text="132|233|424"; var temp = text.replace("|",","); alert(temp); }); expected result is 132,233,424 but its returning 132,233|424 – ravithejag Aug 16 '17 at 07:11
  • You need a regExp to replace all occurrences, based on w3c documentation: Note: If you are replacing a value (and not a regular expression), only the first instance of the value will be replaced. To replace all occurrences of a specified value, use the global (g) modifier (see "More Examples" below). Source: https://www.w3schools.com/jsreF/jsref_replace.asp – Javier del Saz Aug 17 '17 at 08:15
0

You should do:

.replace(/<\/div><div>/g,"<br>");

The RegExp should not be enclosed within quotes else it would be a String. Also, the .val() method is used for textareas. Use .html() for header, divs, etc.

putvande
  • 15,068
  • 3
  • 34
  • 50
Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84
  • Hi, thanks for the response. Sorry I was using input type hidden not textarea, I wrote wrong. However after I change to your code, it still does not work – RickyHalim Dec 11 '14 at 12:33