2

I have this code That is reponsible to load values inside my control

actualy line breaks are appearing , so i am replacing all the line breaks with following code

/* Specific method to load values from a radio group */

load_values.radiogroup = function (ctrl_type, ctrl_id) {            // debugger;
    var form = $("#theForm");
    var div_ctrl = $("#" + ctrl_id);
    var options = '';
    var ctrls = div_ctrl.find("div").find("label");
    var radios = div_ctrl.find("div").find("input");
    ctrls.each(function (i, o) {
        options += $(o).text() + '\n';
    });
    form.find("[name=name]").val(radios[0].name)
    form.find("[name=options]").val($.trim(options.replace(/\n\n/g, "")));
}

this function is reponsible to remove line break,

 form.find("[name=options]").val($.trim(options.replace(/\n\n/g, "")));

But it is not working in firefox

any help with this please

enter image description here

rockerest
  • 10,412
  • 3
  • 37
  • 67
Jot Dhaliwal
  • 1,470
  • 4
  • 26
  • 47

2 Answers2

3

There are a couple of problems:

  1. .replace does not search across multiple lines by default
  2. .replace does not continue past the first match by default
  3. /n is actually preceded by whitespace (the carriage return \r character), so your test for \n\n will never match anything.

If you use a simple regular expression with the global and multiline flags, this will work:

.replace( /(?:\r?\n)+/gm, '\n' )

I've created a small fiddle that demonstrates this, and also removes leading space from a line.

As you can see, I made the leading \r optional, since you indicate it's working in other browsers. You should note that this is probably due to something magic happening in other browsers to normalize: Windows uses \r\n and (most) other platforms use \n.

That said, a visual new line in print is two things: a character to indicate that a new line should be begun, and a character to tell the text caret to return to the beginning of the line (now a brand new one). While in practice a new line is all that most text processors need, the technically correct new line indicator in typography is both \n and \r. This is sort of a fuzzy line between what lines are in the real world and what they are in computers. This is the Windows standard, and I like it because it represents the real thing. On the other hand, Unix and Mac OSX use only \n which I like because it is conceptually easy to remember \n is a new line. Regardless, you need to test for both.

rockerest
  • 10,412
  • 3
  • 37
  • 67
  • @JotDhaliwal I wrote, tested, and posted this answer in Firefox, so I **know** it works in Firefox. If what I posted doesn't strip multiple lines, then something else you have in your code is causing errors. You should create a [MCVE](http://stackoverflow.com/help/mcve) to demonstrate what isn't working. – rockerest Oct 27 '15 at 06:57
  • @JotDhaliwal No, sorry. StackOverflow is a site about creating knowledge for everyone. As a side effect, sometimes individuals can solve their own problems. It's a win-win for everyone. Me helping you on TeamViewer helps no one but you. As a minor sidenote, I charge $275 / hr for that type of help, with a minimum of 1 hour billed: it would be worth your time to expend the effort here to elucidate your problem and get the free help. – rockerest Oct 27 '15 at 07:02
-1

Try it : Use Below regular expression with replace for globally line breaker remover...

.replace( /(?:\n\r?)+/gm, '\n' )
Chilli
  • 123
  • 3
  • 1
    Did you literally copy my code sample and post as your own answer? I have a moderately unusual whitespace style that almost no one I've seen replicates. This is pretty egregious. – rockerest Oct 27 '15 at 06:51