Remove 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

/[|>]/g
productName = productName.replace[/[|>]/g,' '];

That pattern matches

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

etc...

answered Nov 15, 2010 at 13:37

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 //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.0m266 gold badges3249 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,8522 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 you remove a line break in 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. The trim[] method doesn't change the original string, it returns a new string.

How do you skip a line in JavaScript?

The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.

What does \r do in JavaScript?

The RegExp \r Metacharacter in JavaScript is used to find the carriage return character [Carriage return means to return to the beginning of the current line without advancing downward]. If it is found it returns the position else it returns -1.

Chủ Đề