1

If you want to toy with white-spaces, tabs, linefeed and carriage-return, how do you "select them" in javascript?

  x = $('pre').html().replace(/(\r\n|\n|\r)/gm, "#");

edit : ^Does not work for linefeed and carriage-return.

Now, tabs are /(\t)/ and spaces are /( )/.

4

2 回答 2

1

Your example actually works.

http://jsfiddle.net/RSfN5/1/

于 2011-07-07T19:43:12.613 回答
0

You could always escape the string then replace the escaped values - then unescape again - e.g.

<pre>
<script language="javascript" type="text/javascript">
var str = "This is some\nJavascripty stuff with\nlinebreaks";
document.write(str);
var escStr = escape(str);
document.write('<br />');
noLineBreaks = escStr.replace(/%0A/g, "#");
document.write(unescape(noLineBreaks));
</script>
</pre>
于 2011-07-07T19:21:37.350 回答