2011-10-18 6 views
0

ためJavaScriptの正規表現の例

jQuery(".lireArticleAction a").each(function(){ 
       jQuery(this).attr('href',jQuery(this).attr('href').replace(/\/\/(\d)_.(\d{3})\//,'/')); 
     }) 

作品以下このスクリプト:

<span class="lireArticleAction" id="lireArticle"> 
<a href="website/abc/d/e/1_.000/DispForm.aspx?ID=1" class="">read</a> 
     </span> 

リターン:http://website/abc/d/e/DispForm.aspx?ID=1

ではなく、ために:

<span class="lireArticleAction" id="lireArticle"> 
<a href="website/abc/d/e/10_.000/DispForm.aspx?ID=10" class="">read</a> 
     </span> 

返しますhttp://website/abc/d/e/10_.000/DispForm.aspx?ID=10

どうすれば1_.000,10_.000,100_.000,1000_.000などで動作させることができますか?ここ

はjsfiddle http://jsfiddle.net/atoswchataigner/5uh8N/

答えて

2

上のテストが\/\/\d後にプラス記号を追加しています。 \/\/\d+の意味:「//後にすべての連続した数字と一致:代わりにあなたの現在の正規表現の

/\/\/(\d+)_.(\d{3})\// 

は、あなたも使用することができます。

jQuery(this).attr('href',jQuery(this).prop('href').replace(/(?!:)\/\//g,'/')); 

.prop方法ではなく、設定された属性よりも、解析されたURLを返します。 /(?:)\/\//g正規表現では、すべてのスラッシュが1つのスラッシュで置き換えられます(スラッシュの前には、:(プロトコルの後ろに接尾辞):http://)。

1

/\/\/(\d+)_.(\d{3})\//,'/'

+は、1つ以上の数字と一致します。

+0

同じ回答が以前に提案されました:) – user472285

+0

ええ、それを見ていない! –