-4

ワークフローソフトウェアから次のコメント行があります。1行目と残りのコメントから一部を抽出する必要があります。

これがサンプルです

Nelly Thomas (Approve) 12/27/2012 8:50 PM - 12/27/2012 8:52 PM
(Nelly Thomas) LazyApproval by nelly.thomas@joshworld.local Approved

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
when an unknown printer took a galley of type and scrambled it to make a type specimen book

このように抽出する必要があります。

Nelly Thomas 12/27/2012 8:50 PM

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book

これを実現するには正規表現が必要です。

4

1 に答える 1

0

さて、ここに行きます:

var s = "Nelly Thomas (Approve) 12/27/2012 8:50 PM - 12/27/2012 8:52 PM\n\
(Nelly Thomas) LazyApproval by nelly.thomas@joshworld.local Approved\n\
\n\
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, \n\
when an unknown printer took a galley of type and scrambled it to make a type specimen book";
s.replace(/(.+)\(.+\)\s((\d\d\/){2}\d{4}\s\d{1,2}:\d\d\s\w\w)\s-\s.+[\n|\r].+[\n|\r]{2}([^]+)/gi, '$1$2\n\n$4');

//Result: 
"Nelly Thomas 12/27/2012 8:50 PM

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
when an unknown printer took a galley of type and scrambled it to make a type specimen book"

これは有効な正規表現ですが、特にきれいではありません。

# /                   --> Regex start:
# (.+)                --> a word (group #1)
# \(.+\)\s            --> followed by a word in () and a space.
# ((\d\d\/){2}\d{4}\s --> followed by a date and
# \d{1,2}:\d\d\s\w\w) --> time (group #2)
# \s-\s               --> followed by ` - `
# .+                  --> followed by any number of letters or spaces. (The 2nd date)
# [\n|\r]             --> followed by a newline.
# .+                  --> followed by any number of letters or spaces. (The 2nd line)
# [\n|\r]{2}          --> followed by 2 newlines.
# ([^]+)              --> followed by _any_ character, including newlines (group 4)
# /gi                 --> Regex end, (g)lobal flag, case (i)nsensitive flag.

次に、との間に二重改行を入れて、グループとを出力し1ます。2424

したがって、醜いですが、テキストが次の形式に従っている限り、機能します。

W(W)DD \ DD \ DDDD D(?D):DD LL-W
W

W

ここDで、は1桁、Lは文字、Wは改行を除く任意の数の単語とスペースであり、D(?D)1桁または2桁を意味します。

于 2013-01-09T07:51:19.747 に答える