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

使用 javascript 替换方法:

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 回答