How can I replace this character | in JavaScript?
<html>
<body>
<script type="text/javascript">
var str="data|data|data";
document.write(str.replace(/|/g,"<br />"));
</script>
In the output of given code, every character has the “< br />”
I don’t know what is wrong with my code.. 🙂
Also, for PHP, I want the function to use if the input is
|string||
and the output should be
string
only. I want only the outer part of the string in PHP to be subtituted: ||hel||lo|| would become hel||lo.
Could I use trim()? I think trim() only applies to white spaces.
JavaScript:
You have to escape the pipe symbol:
PHP:
You can pass another parameter to
trim()that specifies which characters to remove:If you also want to remove the character in the middle of the string you can use
str_replace():