Replace tags in string with line breaks JS
I am using react and pulling from an api where comments are held. There are multiple comments with br
tags because the api come from a php user. I am trying to replace the br
tags in each string with an actual line break. I am also using react, so jQuery is a no-go.
This is what I have:
this.state.notes.forEach((note) => {
const fixed = note.body.replace(/<br>/g, "rn");
rows.push(<ListGroupItem hover href="#" className="whiter" key=
{note.id}>
<div className="d-flex justify-content-between">
<h5 className="mb-1">{ note.name }</h5>
<small>{ commentDate }</small>
</div>
<p className="mb-1">{ fixed }</p>
</ListGroupItem>)
}
I have replaced it with spaces and it worked, but it hasn't worked with the line break. Where am I going wrong?
Edit 1: @T.J. Crowder I added how it's displayed on the page. rows
is an empty array and the ListGroupItem
's come from MDBootstrap.
javascript reactjs replace line-breaks
|
show 1 more comment
I am using react and pulling from an api where comments are held. There are multiple comments with br
tags because the api come from a php user. I am trying to replace the br
tags in each string with an actual line break. I am also using react, so jQuery is a no-go.
This is what I have:
this.state.notes.forEach((note) => {
const fixed = note.body.replace(/<br>/g, "rn");
rows.push(<ListGroupItem hover href="#" className="whiter" key=
{note.id}>
<div className="d-flex justify-content-between">
<h5 className="mb-1">{ note.name }</h5>
<small>{ commentDate }</small>
</div>
<p className="mb-1">{ fixed }</p>
</ListGroupItem>)
}
I have replaced it with spaces and it worked, but it hasn't worked with the line break. Where am I going wrong?
Edit 1: @T.J. Crowder I added how it's displayed on the page. rows
is an empty array and the ListGroupItem
's come from MDBootstrap.
javascript reactjs replace line-breaks
"I have replaced it with spaces and it worked, but it hasn't worked with the line break." How are you rendering the string once you've modified it?
– T.J. Crowder
Nov 13 '18 at 17:47
You can try with this text.split( "<br>" ).join( "n" )
– Hemadri Dasari
Nov 13 '18 at 17:47
Fundamentally, if you want the text of the comments rather than HTML for the comments, you need to get the other end to send you the text rather than HTML for the text. By sending you HTML, they're giving you a significant amount of work to do. You don't want to just replace<br>
with line breaks, because apparently what you're receiving is HTML, and so may have other aspects of HTML you need to deal with, such as named character entities (<
,&
, etc.) or numeric character entities (such asc;
). You have to worry about XSS and various other issues.
– T.J. Crowder
Nov 13 '18 at 17:48
2
@HemadriDasari - The OP's already got valid code replacing<br>
with line breaks. It's not useful to point them at a different way. :-)
– T.J. Crowder
Nov 13 '18 at 17:49
I'm pretty sure it's not sending me HTML, just strings. The strings containbr
tags that end up showing up in the string. I just want to remove the tags from the string and make them actual line breaks because it doesn't look good how they are separated with just spaces.
– Nathan Geckle
Nov 13 '18 at 17:57
|
show 1 more comment
I am using react and pulling from an api where comments are held. There are multiple comments with br
tags because the api come from a php user. I am trying to replace the br
tags in each string with an actual line break. I am also using react, so jQuery is a no-go.
This is what I have:
this.state.notes.forEach((note) => {
const fixed = note.body.replace(/<br>/g, "rn");
rows.push(<ListGroupItem hover href="#" className="whiter" key=
{note.id}>
<div className="d-flex justify-content-between">
<h5 className="mb-1">{ note.name }</h5>
<small>{ commentDate }</small>
</div>
<p className="mb-1">{ fixed }</p>
</ListGroupItem>)
}
I have replaced it with spaces and it worked, but it hasn't worked with the line break. Where am I going wrong?
Edit 1: @T.J. Crowder I added how it's displayed on the page. rows
is an empty array and the ListGroupItem
's come from MDBootstrap.
javascript reactjs replace line-breaks
I am using react and pulling from an api where comments are held. There are multiple comments with br
tags because the api come from a php user. I am trying to replace the br
tags in each string with an actual line break. I am also using react, so jQuery is a no-go.
This is what I have:
this.state.notes.forEach((note) => {
const fixed = note.body.replace(/<br>/g, "rn");
rows.push(<ListGroupItem hover href="#" className="whiter" key=
{note.id}>
<div className="d-flex justify-content-between">
<h5 className="mb-1">{ note.name }</h5>
<small>{ commentDate }</small>
</div>
<p className="mb-1">{ fixed }</p>
</ListGroupItem>)
}
I have replaced it with spaces and it worked, but it hasn't worked with the line break. Where am I going wrong?
Edit 1: @T.J. Crowder I added how it's displayed on the page. rows
is an empty array and the ListGroupItem
's come from MDBootstrap.
javascript reactjs replace line-breaks
javascript reactjs replace line-breaks
edited Nov 13 '18 at 17:52
Nathan Geckle
asked Nov 13 '18 at 17:42
Nathan GeckleNathan Geckle
304
304
"I have replaced it with spaces and it worked, but it hasn't worked with the line break." How are you rendering the string once you've modified it?
– T.J. Crowder
Nov 13 '18 at 17:47
You can try with this text.split( "<br>" ).join( "n" )
– Hemadri Dasari
Nov 13 '18 at 17:47
Fundamentally, if you want the text of the comments rather than HTML for the comments, you need to get the other end to send you the text rather than HTML for the text. By sending you HTML, they're giving you a significant amount of work to do. You don't want to just replace<br>
with line breaks, because apparently what you're receiving is HTML, and so may have other aspects of HTML you need to deal with, such as named character entities (<
,&
, etc.) or numeric character entities (such asc;
). You have to worry about XSS and various other issues.
– T.J. Crowder
Nov 13 '18 at 17:48
2
@HemadriDasari - The OP's already got valid code replacing<br>
with line breaks. It's not useful to point them at a different way. :-)
– T.J. Crowder
Nov 13 '18 at 17:49
I'm pretty sure it's not sending me HTML, just strings. The strings containbr
tags that end up showing up in the string. I just want to remove the tags from the string and make them actual line breaks because it doesn't look good how they are separated with just spaces.
– Nathan Geckle
Nov 13 '18 at 17:57
|
show 1 more comment
"I have replaced it with spaces and it worked, but it hasn't worked with the line break." How are you rendering the string once you've modified it?
– T.J. Crowder
Nov 13 '18 at 17:47
You can try with this text.split( "<br>" ).join( "n" )
– Hemadri Dasari
Nov 13 '18 at 17:47
Fundamentally, if you want the text of the comments rather than HTML for the comments, you need to get the other end to send you the text rather than HTML for the text. By sending you HTML, they're giving you a significant amount of work to do. You don't want to just replace<br>
with line breaks, because apparently what you're receiving is HTML, and so may have other aspects of HTML you need to deal with, such as named character entities (<
,&
, etc.) or numeric character entities (such asc;
). You have to worry about XSS and various other issues.
– T.J. Crowder
Nov 13 '18 at 17:48
2
@HemadriDasari - The OP's already got valid code replacing<br>
with line breaks. It's not useful to point them at a different way. :-)
– T.J. Crowder
Nov 13 '18 at 17:49
I'm pretty sure it's not sending me HTML, just strings. The strings containbr
tags that end up showing up in the string. I just want to remove the tags from the string and make them actual line breaks because it doesn't look good how they are separated with just spaces.
– Nathan Geckle
Nov 13 '18 at 17:57
"I have replaced it with spaces and it worked, but it hasn't worked with the line break." How are you rendering the string once you've modified it?
– T.J. Crowder
Nov 13 '18 at 17:47
"I have replaced it with spaces and it worked, but it hasn't worked with the line break." How are you rendering the string once you've modified it?
– T.J. Crowder
Nov 13 '18 at 17:47
You can try with this text.split( "<br>" ).join( "n" )
– Hemadri Dasari
Nov 13 '18 at 17:47
You can try with this text.split( "<br>" ).join( "n" )
– Hemadri Dasari
Nov 13 '18 at 17:47
Fundamentally, if you want the text of the comments rather than HTML for the comments, you need to get the other end to send you the text rather than HTML for the text. By sending you HTML, they're giving you a significant amount of work to do. You don't want to just replace
<br>
with line breaks, because apparently what you're receiving is HTML, and so may have other aspects of HTML you need to deal with, such as named character entities (<
, &
, etc.) or numeric character entities (such as c;
). You have to worry about XSS and various other issues.– T.J. Crowder
Nov 13 '18 at 17:48
Fundamentally, if you want the text of the comments rather than HTML for the comments, you need to get the other end to send you the text rather than HTML for the text. By sending you HTML, they're giving you a significant amount of work to do. You don't want to just replace
<br>
with line breaks, because apparently what you're receiving is HTML, and so may have other aspects of HTML you need to deal with, such as named character entities (<
, &
, etc.) or numeric character entities (such as c;
). You have to worry about XSS and various other issues.– T.J. Crowder
Nov 13 '18 at 17:48
2
2
@HemadriDasari - The OP's already got valid code replacing
<br>
with line breaks. It's not useful to point them at a different way. :-)– T.J. Crowder
Nov 13 '18 at 17:49
@HemadriDasari - The OP's already got valid code replacing
<br>
with line breaks. It's not useful to point them at a different way. :-)– T.J. Crowder
Nov 13 '18 at 17:49
I'm pretty sure it's not sending me HTML, just strings. The strings contain
br
tags that end up showing up in the string. I just want to remove the tags from the string and make them actual line breaks because it doesn't look good how they are separated with just spaces.– Nathan Geckle
Nov 13 '18 at 17:57
I'm pretty sure it's not sending me HTML, just strings. The strings contain
br
tags that end up showing up in the string. I just want to remove the tags from the string and make them actual line breaks because it doesn't look good how they are separated with just spaces.– Nathan Geckle
Nov 13 '18 at 17:57
|
show 1 more comment
1 Answer
1
active
oldest
votes
The way you're replacing <br>
is fine, other than you might want to allow for spaces before the closing >
, and an optional /
just before the >
as well:
const fixed = note.body.replace(/<brs*\?>/g, "rn");
But that doesn't solve the problem that what you're getting is HTML, not plain text. You're best off getting the other end to send you the text rather than HTML. By sending you HTML, they're giving you a significant amount of work to do. You don't want to just replace <br>
with line breaks, because HTML can have all sorts of other things in it, such as named character entities (<
, &
, etc.), numeric character entities (such as c;
), script
tags (opening the possibility of XSS attacks), etc. React is smart when you output text and escapes <
and &
and such to that they're output as those characters, not HTML, so if the HTML you receive has, say, <
in it, you'll actually see &
, l
, t
, and ;
in the display, which isn't idea. If your starting point is HTML, interpreting it correctly without allowing scripts and such is difficult. (React does have an option for injecting raw HTML, but it's got an absurdly-awkward name for a reason. :-) )
Once you have the text, with standard line breaks (r
, n
, or rn
), you can render each line in its own div
to get line breaks in the output, since normally in HTML a line break (or any run of whitespace) is just a single space:
<p className="mb-1">{
fixed.split(/[rn]+/).map(line => <div>{line}</div>)
}</p>
Example:
const Example = ({fixed}) => {
return <p className="mb-1">{
fixed.split(/[rn]+/).map(line => <div>{line}</div>)
}</p>;
};
const str =
"This is a string with line breaks.rn" +
"Line 2rn" +
"Line 3";
ReactDOM.render(
<Example fixed={str} />,
document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.4.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.2/umd/react-dom.production.min.js"></script>
1
I see how this would work, but when I.replace(/<brs*\?>/g, "rn")
it doesn't show the "rn
" in the text. It shows the text as if I were replacing it with spaces. Edit: Never mind I failed to call it after the split, so it was still showing the original fixed variable. Thank you for your help!
– Nathan Geckle
Nov 13 '18 at 18:11
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286726%2freplace-br-tags-in-string-with-line-breaks-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The way you're replacing <br>
is fine, other than you might want to allow for spaces before the closing >
, and an optional /
just before the >
as well:
const fixed = note.body.replace(/<brs*\?>/g, "rn");
But that doesn't solve the problem that what you're getting is HTML, not plain text. You're best off getting the other end to send you the text rather than HTML. By sending you HTML, they're giving you a significant amount of work to do. You don't want to just replace <br>
with line breaks, because HTML can have all sorts of other things in it, such as named character entities (<
, &
, etc.), numeric character entities (such as c;
), script
tags (opening the possibility of XSS attacks), etc. React is smart when you output text and escapes <
and &
and such to that they're output as those characters, not HTML, so if the HTML you receive has, say, <
in it, you'll actually see &
, l
, t
, and ;
in the display, which isn't idea. If your starting point is HTML, interpreting it correctly without allowing scripts and such is difficult. (React does have an option for injecting raw HTML, but it's got an absurdly-awkward name for a reason. :-) )
Once you have the text, with standard line breaks (r
, n
, or rn
), you can render each line in its own div
to get line breaks in the output, since normally in HTML a line break (or any run of whitespace) is just a single space:
<p className="mb-1">{
fixed.split(/[rn]+/).map(line => <div>{line}</div>)
}</p>
Example:
const Example = ({fixed}) => {
return <p className="mb-1">{
fixed.split(/[rn]+/).map(line => <div>{line}</div>)
}</p>;
};
const str =
"This is a string with line breaks.rn" +
"Line 2rn" +
"Line 3";
ReactDOM.render(
<Example fixed={str} />,
document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.4.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.2/umd/react-dom.production.min.js"></script>
1
I see how this would work, but when I.replace(/<brs*\?>/g, "rn")
it doesn't show the "rn
" in the text. It shows the text as if I were replacing it with spaces. Edit: Never mind I failed to call it after the split, so it was still showing the original fixed variable. Thank you for your help!
– Nathan Geckle
Nov 13 '18 at 18:11
add a comment |
The way you're replacing <br>
is fine, other than you might want to allow for spaces before the closing >
, and an optional /
just before the >
as well:
const fixed = note.body.replace(/<brs*\?>/g, "rn");
But that doesn't solve the problem that what you're getting is HTML, not plain text. You're best off getting the other end to send you the text rather than HTML. By sending you HTML, they're giving you a significant amount of work to do. You don't want to just replace <br>
with line breaks, because HTML can have all sorts of other things in it, such as named character entities (<
, &
, etc.), numeric character entities (such as c;
), script
tags (opening the possibility of XSS attacks), etc. React is smart when you output text and escapes <
and &
and such to that they're output as those characters, not HTML, so if the HTML you receive has, say, <
in it, you'll actually see &
, l
, t
, and ;
in the display, which isn't idea. If your starting point is HTML, interpreting it correctly without allowing scripts and such is difficult. (React does have an option for injecting raw HTML, but it's got an absurdly-awkward name for a reason. :-) )
Once you have the text, with standard line breaks (r
, n
, or rn
), you can render each line in its own div
to get line breaks in the output, since normally in HTML a line break (or any run of whitespace) is just a single space:
<p className="mb-1">{
fixed.split(/[rn]+/).map(line => <div>{line}</div>)
}</p>
Example:
const Example = ({fixed}) => {
return <p className="mb-1">{
fixed.split(/[rn]+/).map(line => <div>{line}</div>)
}</p>;
};
const str =
"This is a string with line breaks.rn" +
"Line 2rn" +
"Line 3";
ReactDOM.render(
<Example fixed={str} />,
document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.4.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.2/umd/react-dom.production.min.js"></script>
1
I see how this would work, but when I.replace(/<brs*\?>/g, "rn")
it doesn't show the "rn
" in the text. It shows the text as if I were replacing it with spaces. Edit: Never mind I failed to call it after the split, so it was still showing the original fixed variable. Thank you for your help!
– Nathan Geckle
Nov 13 '18 at 18:11
add a comment |
The way you're replacing <br>
is fine, other than you might want to allow for spaces before the closing >
, and an optional /
just before the >
as well:
const fixed = note.body.replace(/<brs*\?>/g, "rn");
But that doesn't solve the problem that what you're getting is HTML, not plain text. You're best off getting the other end to send you the text rather than HTML. By sending you HTML, they're giving you a significant amount of work to do. You don't want to just replace <br>
with line breaks, because HTML can have all sorts of other things in it, such as named character entities (<
, &
, etc.), numeric character entities (such as c;
), script
tags (opening the possibility of XSS attacks), etc. React is smart when you output text and escapes <
and &
and such to that they're output as those characters, not HTML, so if the HTML you receive has, say, <
in it, you'll actually see &
, l
, t
, and ;
in the display, which isn't idea. If your starting point is HTML, interpreting it correctly without allowing scripts and such is difficult. (React does have an option for injecting raw HTML, but it's got an absurdly-awkward name for a reason. :-) )
Once you have the text, with standard line breaks (r
, n
, or rn
), you can render each line in its own div
to get line breaks in the output, since normally in HTML a line break (or any run of whitespace) is just a single space:
<p className="mb-1">{
fixed.split(/[rn]+/).map(line => <div>{line}</div>)
}</p>
Example:
const Example = ({fixed}) => {
return <p className="mb-1">{
fixed.split(/[rn]+/).map(line => <div>{line}</div>)
}</p>;
};
const str =
"This is a string with line breaks.rn" +
"Line 2rn" +
"Line 3";
ReactDOM.render(
<Example fixed={str} />,
document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.4.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.2/umd/react-dom.production.min.js"></script>
The way you're replacing <br>
is fine, other than you might want to allow for spaces before the closing >
, and an optional /
just before the >
as well:
const fixed = note.body.replace(/<brs*\?>/g, "rn");
But that doesn't solve the problem that what you're getting is HTML, not plain text. You're best off getting the other end to send you the text rather than HTML. By sending you HTML, they're giving you a significant amount of work to do. You don't want to just replace <br>
with line breaks, because HTML can have all sorts of other things in it, such as named character entities (<
, &
, etc.), numeric character entities (such as c;
), script
tags (opening the possibility of XSS attacks), etc. React is smart when you output text and escapes <
and &
and such to that they're output as those characters, not HTML, so if the HTML you receive has, say, <
in it, you'll actually see &
, l
, t
, and ;
in the display, which isn't idea. If your starting point is HTML, interpreting it correctly without allowing scripts and such is difficult. (React does have an option for injecting raw HTML, but it's got an absurdly-awkward name for a reason. :-) )
Once you have the text, with standard line breaks (r
, n
, or rn
), you can render each line in its own div
to get line breaks in the output, since normally in HTML a line break (or any run of whitespace) is just a single space:
<p className="mb-1">{
fixed.split(/[rn]+/).map(line => <div>{line}</div>)
}</p>
Example:
const Example = ({fixed}) => {
return <p className="mb-1">{
fixed.split(/[rn]+/).map(line => <div>{line}</div>)
}</p>;
};
const str =
"This is a string with line breaks.rn" +
"Line 2rn" +
"Line 3";
ReactDOM.render(
<Example fixed={str} />,
document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.4.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.2/umd/react-dom.production.min.js"></script>
const Example = ({fixed}) => {
return <p className="mb-1">{
fixed.split(/[rn]+/).map(line => <div>{line}</div>)
}</p>;
};
const str =
"This is a string with line breaks.rn" +
"Line 2rn" +
"Line 3";
ReactDOM.render(
<Example fixed={str} />,
document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.4.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.2/umd/react-dom.production.min.js"></script>
const Example = ({fixed}) => {
return <p className="mb-1">{
fixed.split(/[rn]+/).map(line => <div>{line}</div>)
}</p>;
};
const str =
"This is a string with line breaks.rn" +
"Line 2rn" +
"Line 3";
ReactDOM.render(
<Example fixed={str} />,
document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.4.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.2/umd/react-dom.production.min.js"></script>
answered Nov 13 '18 at 18:00
T.J. CrowderT.J. Crowder
680k12112041299
680k12112041299
1
I see how this would work, but when I.replace(/<brs*\?>/g, "rn")
it doesn't show the "rn
" in the text. It shows the text as if I were replacing it with spaces. Edit: Never mind I failed to call it after the split, so it was still showing the original fixed variable. Thank you for your help!
– Nathan Geckle
Nov 13 '18 at 18:11
add a comment |
1
I see how this would work, but when I.replace(/<brs*\?>/g, "rn")
it doesn't show the "rn
" in the text. It shows the text as if I were replacing it with spaces. Edit: Never mind I failed to call it after the split, so it was still showing the original fixed variable. Thank you for your help!
– Nathan Geckle
Nov 13 '18 at 18:11
1
1
I see how this would work, but when I
.replace(/<brs*\?>/g, "rn")
it doesn't show the "rn
" in the text. It shows the text as if I were replacing it with spaces. Edit: Never mind I failed to call it after the split, so it was still showing the original fixed variable. Thank you for your help!– Nathan Geckle
Nov 13 '18 at 18:11
I see how this would work, but when I
.replace(/<brs*\?>/g, "rn")
it doesn't show the "rn
" in the text. It shows the text as if I were replacing it with spaces. Edit: Never mind I failed to call it after the split, so it was still showing the original fixed variable. Thank you for your help!– Nathan Geckle
Nov 13 '18 at 18:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286726%2freplace-br-tags-in-string-with-line-breaks-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
"I have replaced it with spaces and it worked, but it hasn't worked with the line break." How are you rendering the string once you've modified it?
– T.J. Crowder
Nov 13 '18 at 17:47
You can try with this text.split( "<br>" ).join( "n" )
– Hemadri Dasari
Nov 13 '18 at 17:47
Fundamentally, if you want the text of the comments rather than HTML for the comments, you need to get the other end to send you the text rather than HTML for the text. By sending you HTML, they're giving you a significant amount of work to do. You don't want to just replace
<br>
with line breaks, because apparently what you're receiving is HTML, and so may have other aspects of HTML you need to deal with, such as named character entities (<
,&
, etc.) or numeric character entities (such asc;
). You have to worry about XSS and various other issues.– T.J. Crowder
Nov 13 '18 at 17:48
2
@HemadriDasari - The OP's already got valid code replacing
<br>
with line breaks. It's not useful to point them at a different way. :-)– T.J. Crowder
Nov 13 '18 at 17:49
I'm pretty sure it's not sending me HTML, just strings. The strings contain
br
tags that end up showing up in the string. I just want to remove the tags from the string and make them actual line breaks because it doesn't look good how they are separated with just spaces.– Nathan Geckle
Nov 13 '18 at 17:57