Remove all br from string javascript

I have a string that i would like to remove all occurrences of

I tried this and it did not work.

    productName = productName.replace("
"," ");

However this worked but only for the first

    productName = productName.replace("<br>"," ");

How would I get it to work for all
in the string.

Edit: this is the string...

00-6189 Start Mech Switch<br>00-6189 Start Mech Switch<br>00-6189 Start Mech Switch<br>

My apologies for being a little misleading with the
as it should have been <br>

asked Nov 15, 2010 at 12:23

user357034user357034

10.4k19 gold badges56 silver badges72 bronze badges

1

Using regular expression you can use this pattern

/(<|<)br\s*\/*(>|>)/g
productName = productName.replace(/(<|<)br\s*\/*(>|>)/g,' ');

That pattern matches

 
,
,
,
,
, or <br>, <br/>, <br />

etc...

answered Nov 15, 2010 at 13:37

Remove all br from string javascript

DavideDMDavideDM

1,43515 silver badges18 bronze badges

1

Looks like your string is encoded so use

productName = productName.replace(/<br>/g," ");

note the g after the regular expression which means globally, to match all occurrences.

demo at http://www.jsfiddle.net/gaby/VDxHx/

answered Nov 15, 2010 at 12:33

Gabriele PetrioliGabriele Petrioli

185k34 gold badges254 silver badges306 bronze badges

1

You could use the g flag in your regular expression. This indicates that the replace will be performed globally on all occurrences and not only on the first one.

productName = productName.replace(/\/g," ");

Of course you should be aware that this won't replace
nor
but only
.

See an example of this working on ideone.


UPDATE:

Now that you've provided an example with your input here's a working regex you might use to replace:

productName = productName.replace(/<br>/g, ' ');

answered Nov 15, 2010 at 12:25

Darin DimitrovDarin Dimitrov

1.0m267 gold badges3252 silver badges2911 bronze badges

9

I've not tested it but you could try something like this

productName.replace(/\/g,' ');

answered Nov 15, 2010 at 12:29

m4rcm4rc

2,8622 gold badges21 silver badges29 bronze badges

Remove all Line Breaks from a String in JavaScript #

Use the String.replace() method to remove all line breaks from a string, e.g. str.replace(/[\r\n]/gm, '');. The replace() method will remove all line breaks from the string by replacing them with an empty string.

Copied!

const str = 'a\n multi \n line \r string \n!'; const withoutLineBreaks = str.replace(/[\r\n]/gm, ''); console.log(withoutLineBreaks); // 👉️ a multi line string !

We passed a regular expression to the String.replace method.

The forward slashes / / mark the beginning and end of the regular expression.

Let's first cover the g and m flags at the end of the regex.

The g (global) flag is used to specify that we want to match all occurrences of the regex, and not just the first occurrence.

The m (multiline) flag is used to specify that we want to match occurrences over multiple lines.

The square brackets [] are called a character class and are used to match either of the characters between the brackets.

We want to replace both \r and \n because line breaks vary depending on the operating system.

For example, Windows uses \r\n as end of line character, whereas \n is the default in Unix.

The second parameter we passed to the String.replace method is the replacement for each match.

Copied!

const str = 'a\n multi \n line \r string \n!'; const withoutLineBreaks = str.replace(/[\r\n]/gm, ''); console.log(withoutLineBreaks); // 👉️ a multi line string !

For our purposes, we replace each occurrence of a line break with an empty string.

The String.replace() method doesn't mutate the original string, it returns a new string. Strings are immutable in JavaScript.

Further Reading #

  • Remove all Spaces from a String in JavaScript
  • Replace all Numbers in a String using JavaScript
  • Remove all Numbers from a String in JavaScript
  • Remove Empty Strings from an Array in JavaScript
  • Remove Empty String Values from an Object in JavaScript
  • How to compare Strings in JavaScript
  • How to get the Length of a String in JavaScript
  • Check if Array contains Empty String in JavaScript
  • Interpolate Variable in a String in JavaScript
  • Check if letter in String is Uppercase or Lowercase in JS
  • Check if String contains only Letters and Spaces in JS
  • Check if String contains only Letters and Numbers in JS
  • Remove all non-alphanumeric Characters from String in JS

How do I remove all line breaks from a string?

Use the String. replace() method to remove all line breaks from a string, e.g. str. replace(/[\r\n]/gm, ''); . The replace() method will remove all line breaks from the string by replacing them with an empty string.

Does trim remove line breaks?

trim method removes any line breaks from the start and end of a string. It handles all line terminator characters (LF, CR, etc). The method also removes any leading or trailing spaces or tabs.

What is regex GM?

In the expression /Regex/gm. The "m" stands for multi-line matching. In the expression: m/Regex/g. The "m" stands for "match" as opposed to a substitution, which looks like this: s/Regex/replacement/g. Because matching (vs.