我刚刚写了一个Flex应用程序,它将维基百科的一些文本内容处理为字符串。我正在尝试使用RegExp来清理维基百科的所有标记。下面是一个示例:
我想要这个:
var pageText:String = new String("was an [[People of the United States|American]] [[film director]], writer, [[Film producer|producer]], and [[photographer]] who lived in England during most of the last four decades of his career. Kubrick was noted for the scrupulous care with which he chose his subjects, his slow method of working, the variety of genres he worked in, his technical perfectionism, and his reclusiveness about his films and personal life. He maintained almost complete artistic control, making movies according to his own whims and time constraints, but with the rare advantage of big-[[Movie studio|studio]] [[financial support]] for all his endeavors.");如下所示:
var pageText:String = new String("was an American film director, writer, producer, and photographer who lived in England during most of the last four decades of his career. Kubrick was noted for the scrupulous care with which he chose his subjects, his slow method of working, the variety of genres he worked in, his technical perfectionism, and his reclusiveness about his films and personal life. He maintained almost complete artistic control, making movies according to his own whims and time constraints, but with the rare advantage of big-studio financial support for all his endeavors.");所以我需要写一个[移除这个部分|但保留这个部分]的RegExp。
我测试了以下几个:
var pattern:RegExp = new RegExp(/\[\[(.+)\|/);
var pattern2:RegExp = new regExp(/^\[\[\|/);
var pattern3:RegExp = new RegExp(/^\[\[[A-Z].*\|$/);
var pageTextCleaned:String = pageText.replace(pattern, " ");然后,删除剩余的[和]就很容易了
我根本不习惯这些RegExp的东西,所以任何帮助都会很好!
谢谢!
发布于 2011-03-26 23:27:56
您正在使用RegExp构造函数,该构造函数接受一个字符串作为其参数,但向它提供了一个RegExp。我不认为这是你想要的那样工作。
看看它是否适用于词法RegExp:
var pageTextCleaned:String = pageText.replace(/\[\[([^\]]*\|)?([^\]]+)]]/g, "$2");如果在[[...]]中有单个]或多个|,这不是很健壮,但这是一个开始。
发布于 2011-03-26 23:26:05
我不知道AS3,但这里有一个JavaScript代码来实现它,应该是类似的:
s = s.replace(/\[\[(?:([^\]|]*)|[^\]|]*\|([^\]]*))\]\]/g, '$1$2');正则表达式非常令人困惑。以下是它的各个部分的分解:
\[\[ -两个打开的正方形brackets.(?: | ) -非捕获组,有两个选项:- `([^\]|]*)` - content with does not contain the pipe character, capture the entire content to the first group, `$1`.
- OR
- `[^\]|]*\|([^\]]*)` - link with the pipe character:
- `[^\]|]*` - some characters that are not `]` or `|`.
- `\|` - literal pipe sign.
- `([^\]]*)` - some more non `]` characters, capture into the second group, `$2`.
\[\[ -两个右方括号。然后,我们将每个捕获替换为$1$2 -其中一个始终为空,另一个是我们想要保留的字符串。
工作示例:http://jsbin.com/adedu4
发布于 2011-03-26 23:36:23
由于我不确定最大条目数是否大于2,这里有一个循环的解决方案,将每个以"|“结尾的条目替换为"[[”,直到没有剩余条目,然后删除"[“和"]”。如果总是只有两个,你可以简化一点来加快速度:
var entryPattern:RegExp = new RegExp(/\[\[\w+\|/);
var bracketPattern:RegExp = new regExp(/[\[\[|\]\]]/);
var pageText:String = "your text";
var replacedText:String = "";
while( pageText != replacedText ) {
if( replacedText != "" ){ pageText = replacedText; }
replacedText = pageText.replace(entryPattern, "[[");
}
replacedText = "";
while( pageText != replacedText ) {
if( replacedText != "" ){ pageText = replacedText; }
replacedText = pageText.replace(bracketPattern, "");
}您可能希望将replace循环放入您自己的实用程序"replaceAll“函数中,因为它在任何地方都很方便。
https://stackoverflow.com/questions/5443149
复制相似问题