首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AS3 RegExp问题

AS3 RegExp问题
EN

Stack Overflow用户
提问于 2011-03-26 23:05:02
回答 3查看 606关注 0票数 2

我刚刚写了一个Flex应用程序,它将维基百科的一些文本内容处理为字符串。我正在尝试使用RegExp来清理维基百科的所有标记。下面是一个示例:

我想要这个:

代码语言:javascript
复制
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.");

如下所示:

代码语言:javascript
复制
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。

我测试了以下几个:

代码语言:javascript
复制
           var pattern:RegExp = new RegExp(/\[\[(.+)\|/);
           var pattern2:RegExp = new regExp(/^\[\[\|/);
           var pattern3:RegExp = new RegExp(/^\[\[[A-Z].*\|$/);

           var pageTextCleaned:String = pageText.replace(pattern, " ");

然后,删除剩余的[和]就很容易了

我根本不习惯这些RegExp的东西,所以任何帮助都会很好!

谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-03-26 23:27:56

您正在使用RegExp构造函数,该构造函数接受一个字符串作为其参数,但向它提供了一个RegExp。我不认为这是你想要的那样工作。

看看它是否适用于词法RegExp:

代码语言:javascript
复制
var pageTextCleaned:String = pageText.replace(/\[\[([^\]]*\|)?([^\]]+)]]/g, "$2");

如果在[[...]]中有单个]或多个|,这不是很健壮,但这是一个开始。

票数 4
EN

Stack Overflow用户

发布于 2011-03-26 23:26:05

我不知道AS3,但这里有一个JavaScript代码来实现它,应该是类似的:

代码语言:javascript
复制
s = s.replace(/\[\[(?:([^\]|]*)|[^\]|]*\|([^\]]*))\]\]/g, '$1$2');

正则表达式非常令人困惑。以下是它的各个部分的分解:

  • \[\[ -两个打开的正方形brackets.
  • (?: | ) -非捕获组,有两个选项:

代码语言:javascript
复制
- `([^\]|]*)` - 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

票数 0
EN

Stack Overflow用户

发布于 2011-03-26 23:36:23

由于我不确定最大条目数是否大于2,这里有一个循环的解决方案,将每个以"|“结尾的条目替换为"[[”,直到没有剩余条目,然后删除"[“和"]”。如果总是只有两个,你可以简化一点来加快速度:

代码语言:javascript
复制
 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“函数中,因为它在任何地方都很方便。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5443149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档