Several examples of regular expressions in javascript
! 去除字符串两端空格的处理
如果采用传统的方式,就要可能就要采用下面的方式了
//清除左边空格 function js_ltrim(deststr) { if(deststr==null)return ""; var pos=0; var retStr=new String(deststr); if (retStr.lenght==0) return retStr; while (retStr.substring(pos,pos+1)==" ") pos++; retStr=retStr.substring(pos); return(retStr); } //清除右边空格 function js_rtrim(deststr) { if(deststr==null)return ""; var retStr=new String(deststr); var pos=retStr.length; if (pos==0) return retStr; while (pos && retStr.substring(pos-1,pos)==" " ) pos--; retStr=retStr.substring(0,pos); return(retStr); } //清除左边和右边空格 function js_trim(deststr) { if(deststr==null)return ""; var retStr=new String(deststr); var pos=retStr.length; if (pos==0) return retStr; retStr=js_ltrim(retStr); retStr=js_rtrim(retStr); return retStr; }
采用正则表达式,来去除两边的空格,只需以下代码
String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); }
一句就搞定了,
可见正则表达式为我们节省了相当的编写代码量
! 移动手机号的校验
如果采用传统的校验方式至少就要完成下面三步的校验,
(1). 是否是数字
(2).是否是11位
(3).数字的第三位是否是5,6,7,8,9
如果采用正则表达式校验,只需以下代码
function checkMobile1(form) { if (form.mobile.value > "") { var reg=/13[5,6,7,8,9]\d{8}/; if ( form.mobile.value.match(reg)== null) { alert("请输入正确的移动手机号码!"); form.mobile.focus(); return false; } } return true; }
从上面的代码可以看出校验移动手机号只需定义一个var reg=/13[5,6,7,8,9]\d{8}/;模式匹配串就可以完成合法性校验了
! URL的校验,
条件:必须以http:// 或 https:// 开头, 端口号必须为在1-65535 之间, 以下代码完成了合法性校验
//obj:数据对象 //dispStr :失败提示内容显示字符串 function checkUrlValid( obj, dispStr) { if(obj == null) { alert("传入对象为空"); return false; } var str = obj.value; var urlpatern0 = /^https?:\/\/.+$/i; if(!urlpatern0.test(str)) { alert(dispStr+"不合法:必须以'http:\/\/'或'https:\/\/'开头!"); obj.focus(); return false; } var urlpatern2= /^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?.+$/i; if(!urlpatern2.test(str)) { alert(dispStr+"端口号必须为数字且应在1-65535之间!"); obj.focus(); return false; } var urlpatern1 =/^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(\/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*$/i; if(!urlpatern1.test(str)) { alert(dispStr+"不合法,请检查!"); obj.focus(); return false; } var s = "0"; var t =0; var re = new RegExp(":\\d+","ig"); while((arr = re.exec(str))!=null) { s = str.substring(RegExp.index+1,RegExp.lastIndex); if(s.substring(0,1)=="0") { alert(dispStr+"端口号不能以0开头!"); obj.focus(); return false; } t = parseInt(s); if(t<1 || t >65535) { alert(dispStr+"端口号必须为数字且应在1-65535之间!"); obj.focus(); return false; } } return true; }
对url的校验,看上去有很多的代码,这是因为要给予出错提示, 否则只需var urlpatern1 =/^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(\/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*$/i; 一句就可以校验出url合法性了

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
