1

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.

2
  • 1
    You chould post 2 separate questions (especially in different languages) as, well, 2 seperate questions. Commented Aug 2, 2010 at 9:47
  • Seconded - from your comments below, the PHP question seems to be slightly different from the JS one, but that was not obvious from the question. I'd say those two are different enough for posting as two different questions. Commented Aug 2, 2010 at 9:59

4 Answers 4

4

You don't need a regular expression for this - if you pass a string as the first argument to replace(), it will be replaced literally:

var str='data|data|data';
str = str.replace('|', '');

...but it will only replace the first match. For a global replace, you need to specify the global flag:

var str='data|data|data';
var re = new RegExp('\|','g'); // G is the 'global' flag
str = str.replace(re,'');

In PHP, str_replace() works with string literals:

$str='data|data|data';
$str = str_replace('|', '', $str);
// $str == datadatadata

If you only want to remove the outer delimiters, use trim():

$str='|||data|data||data|';
$str = trim($str,'|');
// $str == 'data|data||data';
Sign up to request clarification or add additional context in comments.

3 Comments

That is not correct. In JavaScript, it will only replace the first occurrence of |, you have to use regular expressions here.
i want only the outer part of the string in php to be subtituted.. like "||hel||lo||" and the output would be "hel||lo"
@vrynxzent: You can use trim() for this, see my answer.
3

JavaScript:

You have to escape the pipe symbol:

document.write(str.replace(/\|/g,"<br />"));
//                       ---^

PHP:

You can pass another parameter to trim() that specifies which characters to remove:

$str = trim($str, '| ');

If you also want to remove the character in the middle of the string you can use str_replace():

$str = str_replace('|', '', $str);

Comments

2

you need to escape the | character because it is used as OR in regex /a|b/ matches either a OR b. Try /\|/

Edit: To achieve wour last goal try doing it this way (alot of regex,I know):

document.write(str.replace(/(\|)+$/g,"").replace(/^(\|)+/g,"").replace(/(\|)+/g,"<br />"));

Comments

1

In Javascript, you'll need to escape that pipe character:

alert("data|data|data".replace(/\|/g,"<br />"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.