织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>杂项技术>Web>

一些字符操作的函数

罗索客 发布于 2006-11-28 09:13 点击:次 
String.prototype.lTrim = function() { return this.replace(/^\\s*/, \"\"); } String.prototype.rTrim = function() { return this.replace(/\\s*$/, \"\"); } String.prototype.trim = function() { return this.lTrim().rTrim(); } //或者 String.prototype.trim=
TAG:

  1. String.prototype.lTrim = function() {
  2. return this.replace(/^\\s*/, "");
  3. }
  4. String.prototype.rTrim = function() {
  5. return this.replace(/\\s*$/, "");
  6. }
  7. String.prototype.trim = function() {
  8. return this.lTrim().rTrim();
  9. }
  10. //或者
  11. String.prototype.trim= function() {
  12. return this.replace(/(^\\s*)|(\\s*$)/g, "");
  13. }
  14. /*** 返回字节数 ***/
  15. String.prototype.lenB = function() {
  16. return this.replace(/[^\\x00-\\xff|·]/g, "**").length;
  17. }
  18. //alert("中国".lenB()); //out 4
  19. String.prototype.stripTags = function() {
  20. return this.replace(/<\\/?[^>]+>/gi, "");
  21. }
  22. String.prototype.escapeXML = function() {
  23. return this.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
  24. }

  1. function XMLEscape(s) {
  2. s = s.replace(/&/g, "&amp;");
  3. s = s.replace(/>/g, "&gt;");
  4. s = s.replace(/</g, "&lt;");
  5. s = s.replace(/"/g, "&quot;");
  6. s = s.replace(/'/g, "&apos;");
  7. return s;
  8. }
  9. function URLEncode(plaintext) {
  10. var SAFECHARS = "0123456789" + // Numeric
  11. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
  12. "abcdefghijklmnopqrstuvwxyz" +
  13. "-_.!~*'()"; // RFC2396 Mark characters
  14. var HEX = "0123456789ABCDEF";
  15. var encoded = "";
  16. for (var i = 0; i < plaintext.length; i++ ) {
  17. var ch = plaintext.charAt(i);
  18. if (ch == " ") {
  19. encoded += "+"; // x-www-urlencoded, rather than %20
  20. } else if (SAFECHARS.indexOf(ch) != -1) {
  21. encoded += ch;
  22. } else {
  23. var charCode = ch.charCodeAt(0);
  24. if (charCode > 255) {
  25. encoded += "+";
  26. } else {
  27. encoded += "%";
  28. encoded += HEX.charAt((charCode >> 4) & 0xF);
  29. encoded += HEX.charAt(charCode & 0xF);
  30. }
  31. }
  32. } // for
  33. return encoded;
  34. }
(iwgh)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/200611/6423.html]
本文出处: 作者:iwgh
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容