Javascript 将href替换为角正则表达式

Javascript 将href替换为角正则表达式,javascript,angularjs,json,regex,ionic-framework,Javascript,Angularjs,Json,Regex,Ionic Framework,我正在寻找一个过滤器来替换API的TextEntity中的“href”。“文本”可以包含3种不同的URL。更换后,我想在一个新的单独窗口中打开更正后的URL 我从textvalue收到以下信息: 1. <a href="http://someurl.tdl">link</a> - this is an example for a all kind of external links. Can be mysite.com/mypage.html or any other v

我正在寻找一个过滤器来替换API的TextEntity中的“href”。“文本”可以包含3种不同的URL。更换后,我想在一个新的单独窗口中打开更正后的URL

我从textvalue收到以下信息:

1. <a href="http://someurl.tdl">link</a> - this is an example for a all kind of external links. Can be mysite.com/mypage.html or any other valid url. Like everything with a http://, https://, ftp:// in the startof the url.
2. <a href="singpage.html">internal page</a> - Can includes all other files. like mypdf.pdf or mydoc.doc or other stuff, but without http://mydomain.tdl
3. <a href="mailto: mymail@domain.tdl">mymail@domain.tdl</a>
我需要这个输出“文本”通过过滤器运行:

1. <a href="http://someurl.tdl" class="externalURL" onClick="cordova.InAppBrowser.open('http://someurl.tdl', '_blank', 'location=yes')">link</a>
2. <a href="http://www.mydomain.tdl/singpage.html" onClick="cordova.InAppBrowser.open('http://www.mydomain.tdl/singpage.html', '_blank', 'location=yes')">internal page</a>
3. <a href="mailto: mymail@domain.tdl">mymail@domain.tdl</a>
1。
2.
3.
为便于理解,请执行以下操作:

我需要一个函数来转换这种类型的URL

<a href="http://someurl.tdl/whichcanincludeanything.html?bar=foo">URL TO A  EXTERNAL PAGE</a>

<a href="singpage.html">internal page of the CMS</a> 

进入


这是一个基于您的原始
代码的版本:

(函数(){
有棱角的
.module('app',['ngSanitize']))
.controller('MainCtrl',MainCtrl)
.filter('parseText',parseText);
函数MainCtrl($scope){
$scope.array=[
'',
'',
'',
'',
''
];
}
函数parseText($sce){
var myDomain=http://www.mydomain.tdl/';
var externalRegex=/([http | https | ftp]+:\/\/[^“]*)/g;
var internalRegex=/(href=)“([^”]*)/g;
返回函数(输入){
如果(!输入)返回;
if(input.indexOf('mailto:')!=-1)返回输入;
var url='';
if(外部规则测试(输入)){
url=input.replace(externalRegex,$1”class=“externalURL”onClick=“cordova.InAppBrowser.open(\'1\',\'u blank\',\'location=yes\');
}否则{
url=input.replace(internalRegex,$1'+myDomain+'$2'+'class=“externalURL”onClick=“cordova.InAppBrowser.open(\''+myDomain+'$2'+'\',\''u blank\',\'location=yes\');
}
返回$sce.trustAsHtml(url);
}
}
})();

Hm, 我现在有一个解决办法

.filter('parseText', function ($sce, $sanitize) {
return function ( text ) {
var mydomain = 'http://www.mydomain.tdl';
text = $sanitize(text);
var source,sear,repl;
var newString;
var regex = /href="([\S]+)"/g;
var extMatch = /http|https|\/\//g;
var mtMatch = /mailto:/g;

var div = document.createElement("div");
div.innerHTML = text;
var aList = div.getElementsByTagName("a");
var aListNew = new Array();
var href,t;

if(aList.length>0){

for(var i = 0; i<aList.length; i++ ){
  href = aList[i].getAttribute("href");
  t = aList[i].outerHTML;
  t = $sanitize(t);

  if(href.match(extMatch)!=null){
    newString = t.replace(regex, "class=\"externalURL\" onClick=\"cordova.InAppBrowser.open('$1', '_blank', 'location=yes')\"");

  } else if(href.match(mtMatch)!=null){
    newString = t;
  } else{
    newString = t.replace(regex, "class=\"externalURL\" onClick=\"cordova.InAppBrowser.open('"+mydomain+"$1', '_blank', 'location=yes')\"");
  }

  source = text;
  sear = t;
  repl = newString;
  text = text.replace(t,newString);
     }   
 }
 return $sce.trustAsHtml(text);
     };
 });
.filter('parseText',函数($sce,$sanitize){
返回函数(文本){
var mydomain=http://www.mydomain.tdl';
text=$sanitize(text);
var来源、sear、repl;
var-newString;
var regex=/href=“([\S]+)”/g;
var extMatch=/http | https | \//\//g;
var mtMatch=/mailto:/g;
var div=document.createElement(“div”);
div.innerHTML=文本;
var aList=div.getElementsByTagName(“a”);
var aListNew=新数组();
var href,t;
如果(列表长度>0){

对于(var i=0;iWell,您确定这是预期的结果吗?对于您已有的代码来说,这毫无意义。我猜我的代码只是用类和onclick事件扩展了a href…并且我还尝试替换href.the href,该href中没有有效的域(intern url)。我的意思是:为什么是1.url和3.url(在您的预期结果中)不会更改
href
?第一个有效,但第二个和第三个无效-第二个是没有域的链接,我尝试在NewsTringur中替换它,第三个不以任何方式由我的代码处理,我也不知道如何处理。好的,我同样无法获得您的逻辑。在第一个示例中,您有:
并且您不想更改它(根据您的预期结果)--第3个示例也是如此。但是在第2个示例中,您想更改
href
。其逻辑是什么?为什么不更改所有3个呢?它们之间的区别是什么?起初-谢谢,但这不是我想要的-在我的情况下,文本不是数组,而是字符串,您的解决方案只能工作对于以“tdl”结尾的域,但外部url也可以是“…/somepage.html”。@Marq,你说过:“文本”可以包含3种url。”,我的工作是基于你所说的。这只是一个例子。过滤器可以处理所有事情。请编辑你的问题,发布你所有的URL,以及预期的结果。sry,我尽了最大努力-但我对javascript和stackoverflow非常陌生:()@Marq,你现在检查过了吗?
<a href="http://someurl.tdl/whichcanincludeanything.html?bar=foo" class="externalURL" onClick="cordova.InAppBrowser.open('http://someurl.tdl/whichcanincludeanything.html?bar=foo', '_blank', 'location=yes')">URL TO A  EXTERNAL PAGE</a>

<a href="http://www.mydomain.tdl/singpage.html" onClick="cordova.InAppBrowser.open('http://www.mydomain.tdl/singpage.html', '_blank', 'location=yes')">internal page</a>
.filter('parseText', function ($sce, $sanitize) {
return function ( text ) {
var mydomain = 'http://www.mydomain.tdl';
text = $sanitize(text);
var source,sear,repl;
var newString;
var regex = /href="([\S]+)"/g;
var extMatch = /http|https|\/\//g;
var mtMatch = /mailto:/g;

var div = document.createElement("div");
div.innerHTML = text;
var aList = div.getElementsByTagName("a");
var aListNew = new Array();
var href,t;

if(aList.length>0){

for(var i = 0; i<aList.length; i++ ){
  href = aList[i].getAttribute("href");
  t = aList[i].outerHTML;
  t = $sanitize(t);

  if(href.match(extMatch)!=null){
    newString = t.replace(regex, "class=\"externalURL\" onClick=\"cordova.InAppBrowser.open('$1', '_blank', 'location=yes')\"");

  } else if(href.match(mtMatch)!=null){
    newString = t;
  } else{
    newString = t.replace(regex, "class=\"externalURL\" onClick=\"cordova.InAppBrowser.open('"+mydomain+"$1', '_blank', 'location=yes')\"");
  }

  source = text;
  sear = t;
  repl = newString;
  text = text.replace(t,newString);
     }   
 }
 return $sce.trustAsHtml(text);
     };
 });