0

href の URL を、その URL を含める必要がある関数の呼び出しに置き換えたいと考えています。

例: 次の文字列があります。

<a href="www.google.com">Google</a>
some other text
<a href="www.wikipedia.org">Wikipedia</a>

次のような文字列を取得したい:

<a href="javascript:anyFunction('www.google.com');">Google</a>
some other text
<a href="javascript:anyFunction('www.wikipedia.org')">Wikipedia</a>

RegEx でいくつかの方法をテストしましたが、RegEx は苦手です。誰かが私の問題の解決策を持っていますか?

編集:すみません、書くのを忘れていました。appcelator アプリケーションを作成しています。jQueryまたは「ドキュメント」を使用できません。唯一の方法は正規表現だと思います。

4

3 に答える 3

1

この正規表現を試してみてください:

/href="([^"]+)/g

これはその使用例です(JsFiddle Demo

var subject = '<a href="www.google.com">Google</a>some other text<a href="www.wikipedia.org">Wikipedia</a>';
var result = subject.replace(/href="([^"]+)/g, 'href="javascript:anyFunction(\'$1\')');
于 2012-04-12T22:22:05.280 に答える
0

新着:

JavaScript を使用できる場合、アンカー タグ自体にアクセスできますか? もしそうなら:

anchor_element.href = "javascript:anyFunction('" + anchor_element.href + "')";

年:

<a id="link1" href="www.google.com">Google</a>
some other text
<a id="link2" href="www.wikipedia.org">Wikipedia</a>

<script>
document.getElementById('link1').addEventListener('click', function(e) {
    alert('hello');
    e.preventDefault();
    return false;
});
</script>
于 2012-04-12T21:33:52.033 に答える