2014-10-19 21 views
0

我正在使用Codemirror構建一個帶有一些不錯功能的文本編輯器,如語法突出顯示,使用simple mode中的語言模式,並嘗試編寫幾個正則表達式來匹配段落的第一個和最後一個單詞。例如,在:如何在Codemirror中使用RegEx來匹配段落的第一個和最後一個單詞?

One 
is the first one 
of all the numbers 

Two is 
the second 

第一個正則表達式(在第1號字)應符合OneTwo,而第二個(段最後一個字)應符合numberssecond

我一直在嘗試不同的表達式,但最終找不到合適的表達式。這是我得到的最接近,但仍然顯然不正確:

/[^\r\n]+[a-zA-Z]\s/ 

任何人都可以指出我在正確的方向嗎? 在此先感謝。

回答

1

使用記事本++和rubular.com

查找內容測試:^([A-Z]\w+)(\s\w+)?((\r?\n|\r)([a-z]+\s)+)+(\w+)$

替換爲:\1\n\6

輸入

One 
is the first one 
of all the numbers 

Two is 
the second 

Three is 
the third 

Four is 
the fourth 
of all the numbers 

輸出

One 
numbers 

Two 
second 

Three 
third 

Four 
numbers 

Rubular

enter image description here

+0

此正則表達式在Codemirror中不起作用。請注意,這個問題是特定於CodeMirror的,而不是一般的Regex。 – protozoo 2014-10-22 12:05:57

0

我知道這個問題已經開了一段時間,但想分享,對於CodeMirror正則表達式,通常的 '^'不能用於行/字符串匹配的開始。但是,{正則表達式:...,sol:true}正常工作。 例如in defineSimpleMode() 「sol」代表「行首」

相關問題