2014-06-17 31 views
0

我試圖做一個腳本,在挪威識別phonenumbers,基本上格式是基於8位數的數字,但也應該允許5位數字以及110,112,113(這是挪威的緊急號碼)。腳本beneeth完成這項工作非常完美,但也會影響符合豁免的url和id。我試着用各種方法使這隻影響標籤內部(不正確的標籤本身)。Phonenumber identification add tag

var regex = /((\d){5})|(110)|(113)|(112)|(\d{2}(?: ?\d){6})/g; //\>+. 
text = $("article:first").html().replace(regex, "<a href=\"tel:\" class='phoneme'>$&</a>"); 
$("article:first").html(text); 

有沒有人有任何想法如何做到這一點?

UPDATE:

我指定我的問題有點多:該函數的一點是要取代所有PHONENUMBERS,在文中給出,與鏈接。我使用HTML的原因是爲了保留文章標籤內的html,儘管我不希望標籤內的屬性受到影響。

硒小提琴爲例

http://jsfiddle.net/LFceL/

+1

如果你只想要替換的文本,你爲什麼用'的.html()'? –

+0

嗨,我會指出我的問題多一點: –

+0

看到更新與小提琴 –

回答

0

我已經解決了這個通過創建以下func灰。不是真的我想要的,但它像一個光芒:)

var runPhones = function(elm) { 

    var regex = /((\d){5})|(110)|(113)|(112)|(\d{2}(?: ?\d){6})/g; 
    $("article:first *").contents().filter(function(i,e){ // Filter away all tags 
     if(e.nodeType !== 1){ 
      e.nodeValue = e.nodeValue.replace(regex, "TEL$&TELEND")// Creating markup in text 
     } 
    }); 

    var text = $("article:first").html().replace(/TELEND/g,"</a>"); // Changeing markup to HTML 
    text = text.replace(/TEL/g,"<a href='tel:' class='phoneme'>") // Changing markup to HTML 

    $("article:first").html(text); 

} 
runPhones(); 

我可能會做這種unnessesary複雜,但我diddent找到任何方式在nodeValue.replace直接解析HTML。

SE搗鼓演示

http://jsfiddle.net/LFceL/10/

0

檢查以下script.you可以.mask

改變試試這個:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script type="text/javascript" src="http://cdn.jsdelivr.net/jquery.maskedinput/1.3.1/jquery.maskedinput.js"></script> 
    <script> 
     jQuery(function($) 
       { 
       $.mask.definitions['~']='[+-]';  
       $('#W2TxtCell').mask('(999)(99999)');  
       }); 


    </script> 

<input type="" id="W2TxtCell"/> 

http://jsfiddle.net/Mr7itsurdeveloper/2Xz8q/

+0

糟糕的格式:)爲什麼腳本標記內有一個html標記? +它不允許110,112,113 – VDP

+0

錯誤地添加。現在,我已經更新了我的答案。 –

+0

OP要求在文本中標識(查找)電話號碼以便用直接呼叫的鏈接(使用移動設備)替換它們,有效的電話號碼有3,5或8個號碼。這就是我的理解...... – VDP

相關問題