javascriptはよくユーザー定義の文字列処理関数を使います.
15162 ワード
以下はweb開発でよく使われている文字列処理関数です.
- /*
-
- *
-
- * @author yifangyou
-
- * @use " bv ".trim()
-
- * @version gslb 2011-01-16
-
- * */
-
- String.prototype.trim = function()
-
- {
-
- return this.replace(/(^\s*)|(\s*$)/g, "");
-
- }
-
-
-
-
-
-
-
- /*
-
- *
-
- * @author yifangyou
-
- * @use " bv a".endswith("a")
-
- * @version gslb 2011-01-16
-
- * */
-
- String.prototype.endswith = function(findstr)
-
- {
-
- if(this.length-findstr.length<0){
-
- return false;
-
- }
-
- if(this.lastIndexOf(findstr)==this.length-findstr.length){
-
- return true;
-
- }
-
- return false;
-
- }
-
-
-
-
-
- /*
-
- * .-
-
- * @param str input value
-
- * @use <input onkeyup="value=filterchar(value)" onafterpaste="value=filterchar(value)" />
-
- * @author yifangyou
-
- * @version gslb 2011-01-16
-
- * */
-
- function filterchar(str){
-
- var p=/[^\w\-\.]/g
-
- return str.replace(p,'');
-
- }
-
-
-
- /*
-
- *
-
- * @param str input value
-
- * @use <input onkeyup="value=filterschar(value)" onafterpaste="value=filterschar(value)" />
-
- * @author yifangyou
-
- * @version gslb 2011-01-16
-
- * */
-
- function filterschar(str){
-
- var p=/[\'\"#,;&%^\$!\+=\|\\\/ ]/g
-
- return str.replace(p,'');
-
- }
-
-
-
- /*
-
- * ,\
-
- * @param str input value
-
- * @use <input onkeyup="value=filterschar2(value)" filterschar2="value=filterschar2(value)" />
-
- * @author yifangyou
-
- * @version gslb 2011-01-16
-
- * */
-
- function filterschar2(str){
-
- var p=/[\'\"#,;&%^\$!\+=\|\\ ]/g
-
- return str.replace(p,'');
-
- }
-
-
-
- /*
-
- *
-
- * @param str input value
-
- * @use <input onkeyup="value=filterchinese(value)" onafterpaste="value=filterchinese(value)" />
-
- * @author yifangyou
-
- * @version gslb 2011-01-16
-
- * */
-
- function filterchinese(str){
-
- var p=/[\u4E00-\u9FA5\'\",&%^\$!\+=\|\\\/]/g
-
- return str.replace(p,'');
-
- }
-
-
-
- /*
-
- * "_" "-"
-
- * @param str input value
-
- * @use <input onkeyup="value=filterschar2(value)" filterschar2="value=filterOnlyCE(value)" onafterpaste="value=filterOnlyCE(value)"/>
-
- * @author yifangyou
-
- * @version gslb 2011-01-16
-
- * */
-
- function filterOnlyCE(str){
-
- var p=/[^\u4E00-\u9FA5\w\-]/g
-
- return str.replace(p,'');
-
- }
-
-
-
-
-
-
-
- /*
-
- *
-
- * @param str input value
-
- * @use <input onkeyup="value=filterNumeric(value)" onafterpaste="value=filterNumeric(value)" />
-
- * @author yifangyou
-
- * @version gslb 2011-01-16
-
- * */
-
- function filterNumeric(str){
-
- var p=/[\D]/g
-
- return str.replace(p,'');
-
- }