AS3正则表达式的问题 [英] AS3 RegExp question

查看:176
本文介绍了AS3正则表达式的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚写了一个Flex应用程序它可以处理一些维基百科内容的文本字符串。 我试图用正则表达式来CLEEN所有维基百科的标记。下面是一个例子:

I just wrote a Flex app which handles some Wikipedia text content as strings. I'm trying to use RegExp to cleen all the Wikipedia markup. Here is an example:

我想这样的:

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.");

看起来是这样的:

to look like this:

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.");

所以,我需要写一个正则表达式其中[删除这部分|但要保持这一]。

So I need to write a RegExp which [[ Remove this part | but keep this one ]].

我这些的在其他测试:

           var pattern:RegExp = new RegExp(/\[\[(.+)\|/);
           var pattern2:RegExp = new regExp(/^\[\[\|/);
           var pattern3:RegExp = new RegExp(/^\[\[[A-Z].*\|$/);

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

然后,它会很容易,只是除去剩余的[[和]]

Then it would be easy to just remove the remaining [[ and ]]

我不习惯在这个RegExp的东西好,所以任何帮助将是巨大的!

I'm not used at all with this RegExp stuff, so any help would be great!

谢谢!

推荐答案

您正在使用正则表达式的构造函数,它接受一个字符串作为它的参数,但喂养它一个正则表达式。我不认为这可以作为你想要的。
看看它的工作原理与词汇正则表达式:

You are using the RegExp constructor which takes a string as its argument, but feeding it a RegExp. I don't think that works as you want.
See if it works with a lexical RegExp:

var pageTextCleaned:String = pageText.replace(/\[\[([^\]]*\|)?([^\]]+)]]/g, "$2");

这是不稳健的,如果你有一个] s或多个 | S内部的 [...]] S,但它是一个开始。

This isn't robust if you've got single ]s or multiple |s inside the [[...]]s, but it's a start.

这篇关于AS3正则表达式的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆