有一段純文字text
, 欲將其插入dom節點div
中.text
中可能有超連結, 郵件位址等. 如果有, 識別之.
如果只是純文字, 插入div
中, 只要將div.innertext
設定為text
即可.
text
中的uri位址可以用正則識別, 並將其替換為標籤組成的字串. 此時
text
變成了html字串html
.
html字串html
可以賦值給div.innerhtml
. 但如果原text
中存在html語義的 字串呢? 因此, 在識別uri之前, 需要將原text
作轉義.
uri-recognition.js
(function ()
// s 空格
// 製表符
// xa0 non-breaking spaces
// 3000中文空格
return string(s).replace(/^[s xa03000]+/, '').
replace(/[s xa03000]+$/, '');
},startswith = function (s, sub) ,
test = function (str) ,
/*** @param str
* @param replacer
*/replace = function (str, replacer) );
/*jslint eqeq: true*/
return newstr == null ? match : newstr;
});str = str.replace(mail_reg, function (match) );
/*jslint eqeq: true*/
return newstr == null ? match : newstr;
});return str;
},urirecognition = function (text) to escape
text = text.replace(//g, '}').
replace(//g, '}').
replace(//g, '');
html = replace(text, function (info)
var link = doc.createelement('a');
link.setattribute('href', info.fulluri);
/*jslint eqeq: true*/
if (link.innertext != null) else if (link.textcontent != null)
return link.outerhtml;
});html = html.replace(//g, '<').
replace(//g, '>');
return ;
}return ;
},setcontentwithurirecognition = function (el, text)
if (result.isplaintext) else if (el.textcontent != null)
} else
};window.urirecognition = urirecognition;
window.setcontentwithurirecognition = setcontentwithurirecognition;
})();
test.html
chrome下測試ok.
Java把Html格式文字轉換成純文字的問題
工作上需要把一段html內容轉換成純文字。最簡單的辦法,是用正規表示式把全部的標籤替換成空字串,然後把被轉義的內容 尖括號,空格等 轉回來。這裡用到了stringescapeutils,來自apache commons lang library commons lang3 3.0.1.jar str...
用PHP自動把純文字轉換成Web頁面
set source file name and path source toi200686.txt read raw text as array raw file source or die cannot read file retrieve first and second lines titl...
Oracle中IP位址和掩碼轉換成CIDR格式
遇到的問題如下 資料庫中儲存了ip位址,以及ip位址掩碼,需要將他們轉化成cidr格式的,並且不僅僅是將掩碼轉化成cidr對應的數字的問題,需要將原有的ip位址轉化成對應的網路位址,例如ip位址是58.247.221.238,掩碼是255.255.255.252,需要將其轉化為58.247.221....