3

我想做一个正则表达式,每 2 个单词插入一个回车符......而且要格式化的字符串在一个数组中(这有什么区别吗?)问题是:我在 2 周前和到目前为止听说过正则表达式这让我觉得有人在乱踩我的大脑。所以暂时我想出了那个(忍受我......)

ticketName[i] = 'Lorem ipsum dolor sit amet consectetur adipisicing';
ticketName[i] = ticketName[i].replace(/(?:[a-zA-Z])* {2}/ig, ',');

令人惊讶的是......它不起作用!

谁能帮我?

此外,当我完成此操作后,当句子超过一定数量的字符时,我需要用“...”替换句子的结尾。如果您对此有任何见解,因为我可能还会回来找那个...搜索该网站,只发现用回车替换句子的结尾。我猜我的 pb 在于这个词的定义。谢谢一堆

4

5 回答 5

2

被宠坏了,你是。我只是想我会再添加一个选项,并同时回答您的第二个问题:

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

注意 - 最后一个词('adipisicing')不匹配,所以没有被替换。它就在最后(并且没有尾随\n)。

解释:

正则表达式:

(     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

第二个问题:

顺便说一句 - 由于您询问的是“其他”问题 - 如何在...一定数量的单词后终止句子,您可以执行以下操作:

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

那里-一个问题有两个答案!

如果你想两者都做,那么先做“缩写一个长句子”,然后把它切成更短的片段。使用正则表达式计算多行中的单词有点困难。

于 2013-04-05T17:08:03.810 回答
1

我认为这应该有效:

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

例如:

var str = 'Lorem ipsum dolor sit amet consectetur adipisicing';
str.replace(/((\s*\w+\s+){2})/ig, "$1\n");
"Lorem ipsum 
dolor sit 
amet consectetur 
adipisicing"

正则表达式的解释:

(         -> 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.

在替换中,我们$1\n简单地说“使用第一个捕获组并附加换行符”。

小提琴

于 2013-04-05T16:04:33.610 回答
1

我希望您发现此处发布的正则表达式解决方案之一适合您。

或者,同样可以使用字符串 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);
于 2013-04-05T16:12:32.193 回答
0

让我们从您的正则表达式中的问题开始:
-[a-zA-Z]您不需要指定A-Z,因为您正在使用i修饰符
-{2}正如 Matt Ball 在评论中所述,您只是想在这里匹配 2 个连续的空格,您会需要使用括号来重复整个组
-','你说你想用回车符 (\r) 替换,但是你插入了一个逗号

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

/\b[a-z]+\b/

这里是 \b,我们将使用惰性运算符
你可以这样做:

replace(/(\b[a-z]+\b.*?\b[a-z]+\b)/, '$1\r')
于 2013-04-05T16:00:12.353 回答
0

尝试:

'Lorem ipsum dolor sit amet consectetur adipisicing'
  .match(/([a-z]+\s){2,2}/gi)
  .join('\n');
于 2013-04-05T16:00:15.150 回答