使用正则表达式和javascript每隔2个字插入回车符

使用正则表达式和javascript每隔2个字插入回车符,javascript,regex,insert,return,Javascript,Regex,Insert,Return,我想做一个正则表达式,每两个单词插入一个回车。。。 另外,要格式化的字符串位于数组中(这有什么区别吗?) 问题是:我两周前就听说过regex,到目前为止,它让我觉得有人在随意踩我的脑袋。 所以就在那一刻,我想到了这个(请容忍我…) 令人惊讶的是……它不起作用 有人能帮我吗 同样,当我完成此操作时,我需要在句子超过一定数量的字符时,用另一个字符替换句子末尾的“…”。如果你对此有什么见解的话,因为我很可能也会回来参加那个。。。 搜索该网站,只发现用回车替换句尾。我猜我的pb在于这个词的定义。 非常感

我想做一个正则表达式,每两个单词插入一个回车。。。 另外,要格式化的字符串位于数组中(这有什么区别吗?) 问题是:我两周前就听说过regex,到目前为止,它让我觉得有人在随意踩我的脑袋。 所以就在那一刻,我想到了这个(请容忍我…)

令人惊讶的是……它不起作用

有人能帮我吗

同样,当我完成此操作时,我需要在句子超过一定数量的字符时,用另一个字符替换句子末尾的“…”。如果你对此有什么见解的话,因为我很可能也会回来参加那个。。。 搜索该网站,只发现用回车替换句尾。我猜我的pb在于这个词的定义。
非常感谢

让我们从正则表达式中的错误开始:
-
[a-zA-Z]
在使用i修饰符时,不需要指定
a-Z
-
{2}
正如马特·鲍尔在评论中所说的那样,您在这里只尝试匹配两个连续的空格,您需要使用括号来重复整个组
-
,“
您说过要用回车符(\r)替换,但您要插入一个逗号

现在,匹配单词的最佳方法(在我看来)是:

/\b[a-z]+\b/
var abbrev = mytest.replace(/((\w+\W+){5}).*$/,"$1...");
alert(abbrev);

result:
lorem ipsum dolor sit amet ...
(对于\b,我们将使用)
你可以这样做:

replace(/(\b[a-z]+\b.*?\b[a-z]+\b)/, '$1\r')
尝试:


我认为这应该奏效:

str.replace(/((\s*\w+\s+){2})/g, "$1\n");
例如:

正则表达式的解释:

(         -> start of capture group. We want to capture the two words that we match on
(         -> start of an inner capture group. This is just so that we can consider a "word" as one unit.
\s*\w+\s+ -> I'm consider a "word" as something that starts with zero or more spaces, contains one or more "word" characters and ends with one or more spaces.
)         -> end of capture group that defines a "word" unit.
{2}       -> we want two words
)         -> end of capture group for entire match.
在replace中,我们有
$1\n
,它只是说“使用第一个捕获组并附加一个换行符”


.

我希望您能找到这里发布的其中一个正则表达式解决方案适合您

或者,也可以使用string split()和substring()方法来实现。下面的代码实现了这一点,并提供了一个提琴。不过,这远不如使用正则表达式那么优雅

示例源代码

ticketName = new Array();
ticketName[0] = 'Lorem ipsum dolor sit amet consectetur adipisicing';
tmparray = new Array();
tmparray = ticketName[0].split(" ");



finalName = ""; // hold final name
totallength=0;
maxlength = 30;

// add a carriage return (windows) every two words
for (var i=0; i<tmparray.length; i++){
    finalName += tmparray[i]+" ";
    if ((i>0) && (((i-1)%2) == 0))
        finalName +="\r\n";
}

// truncate result if exceed final length and add .... where truncated
if (finalName.length>maxlength)
    finalName= finalName.substring(0,maxlength)+ ".....";

alert(finalName);
ticketName=newarray();
ticketName[0]=“Lorem ipsum door sit amet concertetur adipising”;
tmparray=新数组();
tmparray=ticketName[0]。拆分(“”);
最终名称=”;//姓
总长度=0;
最大长度=30;
//每两个单词添加一个回车符(windows)
对于(var i=0;i0)和((i-1)%2)==0))
最终名称+=“\r\n”;
}
//如果超过最终长度,则截断结果并添加。。。。哪里被截断了
如果(最终名称长度>最大长度)
finalName=finalName.substring(0,maxlength)+“…”;
警报(最终名称);

你已经被宠坏了,可以选择了。我只是想再增加一个选项,同时回答你的第二个问题:

var mytest = "lorem ipsum dolor sit amet consectetur adipisicing";
var newtext = mytest.replace(/(\w+\W+\w+)\W+/ig,"$1\n");
alert(newtext);

result:
lorem ipsum
dolor sit
amet consectetur
adipisicing
注意-最后一个单词(“adipising”)不匹配,因此未替换。它就在末尾(没有尾随的
\n

说明:

(    start capture group
(    start second group (to be used for counting)
\w+  one or more "word" characters
\W+  one or more "non word" characters
){5} match exactly five of these
)    end of capture group
.*$  followed by any number of characters up to the end of the string
Regexp:

(     start a capture group comprising:
\w+   one or more "word" characters
\W+   one or more "not a word" characters
\w+   one or more "word" characters
)     end capture group
\W    followed by non-word
/ig   case insensitive, global (repeat as often as possible)
并替换为:

$1    "The thing in the first capture group (everything matched in parentheses)
\n    followed by a newline
第二个问题:

顺便说一句,既然你问的是“其他”问题,那么在
中,在一定数量的单词之后,如何结束一个句子,你可以做以下操作:

/\b[a-z]+\b/
var abbrev = mytest.replace(/((\w+\W+){5}).*$/,"$1...");
alert(abbrev);

result:
lorem ipsum dolor sit amet ...
说明:

(    start capture group
(    start second group (to be used for counting)
\w+  one or more "word" characters
\W+  one or more "non word" characters
){5} match exactly five of these
)    end of capture group
.*$  followed by any number of characters up to the end of the string
并替换为

$1   the contents of the capture group (the first five words)
...  followed by three dots
一个问题有两个答案


如果你想同时做这两件事,那么先做“缩写长句”,然后把它分成较短的部分。使用正则表达式跨多行计算单词有点困难。

`2}`匹配2个空格。正则表达式中的回车符在哪里?@silentboy,嗯,在测试时我使用了昏迷,因为我不能100%确定如何执行回车符。。。我认为它是\r或\n…但在测试时,最好不要使用逗号分隔函数参数。实际上,你给了replace函数3个参数。为了给逗号作为论点,你必须用斜杠来逃避它。非常感谢每一个人!!!有关regex如何工作的答案和详细解释。很棒的社区!目前还没有一个能马上起作用,但我想我可能做错了什么。我将在周末继续我的测试。周一回到论坛:)好吧,在这一次我宁愿使用正则表达式。但是谢谢,因为我不知道这在js中是可能的。它可能会在其他用途上派上用场。