2016-04-29 6 views
0

に線を破る方法:私のJSファイルに次のように私は<em>隠さ</em>という変数を持っているのJavascript

var hidden=$('#hidden').val(); 

私はを隠さ変数を警告すると、出力は

Need for voice connection\n\ with text messaging pack\n\ and 3G data 

しかし、私です必要な出力:

Need for voice connection 
with text messaging pack 
and 3G data 

それを達成する方法は? Javascriptを

+0

以下のようにテキスト領域をhidden'のですか? –

+0

*文字通り*の文字列には文字列 '\ n'が含まれています。なぜ、なぜ実際の改行が含まれていないのですか? –

+0

@FelixKling - HTML属性値です。 – Quentin

答えて

0

ではこれが私の作品:

alert('test\\n\\test2\\n\\test3\\n\\test4'.replace(new RegExp(/\\n\\/g), '\n');

私はすべてが非エスケープ文字で\n要素をexcaped交換しています。

+0

これは、最初の '' \ n'を置き換えるだけで、置換するシーケンスはとにかく\ nです。 。おそらく、 '\ n \ 'シーケンスがないOPの文字列を使用してテストするべきでしょう – Quentin

+0

編集後、正確に2つのインスタンスが置き換えられます(' '\' \ 'の後ろに)。何か一般的なものを書くのはなぜですか? – Quentin

+0

正しい@Quentinです。編集されました。 – tilz0R

2

だからあなたは新しい行を表すために\n\を含む文字列を持っていますか?

これらの文字を実際の改行に置き換えます。

var data = "Need for voice connection\\n\\ with text messaging pack\\n\\ and 3G data"; 
 
alert("Original: " + data); 
 
data = data.replace(/\\n\\ /g, "\n"); 
 
alert("Replaced: " + data);

+0

@ Quentin-パーフェクト。その働き。JAVAでの回避策は何ですか? – Satish

+0

私は2003年頃からJavaを書くのをやめました。別の構文を使っていても(おそらく、*正確に*あなたが文字列を使って何をしているかによるが)回避することは同じだろう。 – Quentin

0

期待どおりに出力があり、しなければならないテキスト領域を持つに問題はありません、あなたは改行でいくつかの文章を入力。ここであなたはあなたの所望の出力を得るためにいくつかの余分な努力でデモ..

$('#viewValue').on('click', function() { 
 
    var hidden = $('#hidden').val(); 
 
    alert(hidden); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<textarea id="hidden">Need for voice connection 
 
with text messaging pack 
 
and 3G data</textarea> 
 

 
<button id="viewValue">view</button>


あなたは文字通り文字\nを入力し、改行を期待しているすべての場合を置く必要がありますされます。

正規表現を使用して改行文字で\nを交換してください。あなたの `#デモ

$('#viewValue').on('click', function() { 
 
    var hidden = $('#hidden').val().replace(/\\n/g, "\n"); 
 
    debugger; 
 
    alert(hidden); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<textarea id="hidden">Need for voice connection \n with text messaging pack \n and 3G data</textarea> 
 

 
<button id="viewValue">view</button>

関連する問題