0

私は次のように見える長い文字列を持っています

var myString = "Bob is the best person in the world MAN John is the best person in the world MAN Peter likes to pick apples daily MAN Fred loves to eat bananas MAN"

私がやりたいのは、それぞれの後に新しい行を挿入することですMAN-基本的に

Bob is the best person in the world MAN 
John is the best person in the world MAN 
Peter likes to pick apples daily MAN
Fred loves to eat bananas MAN

どうすればそれを行うことができますか?

4

3 に答える 3

2
myString = myString.replace(/MAN/g, "MAN\n");

/*
 returns
 "Bob is the best person in the world MAN\  (\ means new line)
 John is the best person in the world MAN\
 Peter likes to pick apples daily MAN\
 Fred loves to eat bananas MAN"
*/
于 2013-03-05T08:35:40.727 に答える
1

javascriptreplaceメソッドを使用します。

var n=myString.replace(/MAN/g,"MAN\n"); 

文字列をhtmlとして出力する場合は、

"<br>" instead of "\n".
于 2013-03-05T08:36:45.977 に答える
1
myString = myString.replace(/MAN/g, "MAN\n");

HTML出力の場合は、代わりにこれを使用してください。

myString = myString.replace(/MAN/g, "MAN<br>");
于 2013-03-05T08:37:08.587 に答える