Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize that the input box can only have numbers, Chinese and English commas?

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how regular replacement can only have numbers, Chinese and English commas in the input box. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Core code:

Function renumdou (str) {var regexp = / [^\ d return newstr,] * / GTX newstr.replace (regexp, "); replace}

The editor will share with you another good code:

Automatic detection of digital replacement digital regular expressions

New Document function check (obj) {var sreg = / ^-+. * / g; var zero = / ^ 0 [1-9] +\. *\ if (sreg.test (val)) {val = val.replace (/-+ / g camera') Plus ='-';} val = val.replace (/\ s); if (/ ^\. +. * $/ .test (val)) {val =';} val = val.replace (/ [^\ d\.] /,'') Val = val.replace (/ (^\ d +\. {1}) (\ d *). * / GMagna / 1 $2'); val = val.replace (/ (^\ d +\.\ d {3})\ dThink / val.replace (/ ^ [0] * (0 {1}) ([1-9] *) (. *) / GMagazine / 1 $2 $3')) If (zero.test (val)) {val = val.replace (/ 0 ([1-9] +) (. *) $/,'$1 $2');} obj.value = plus+val;}

Instructions for using RegExp

One: the way regular expressions are created

1. Text format, the usage is as follows:

/ pattern/flags (i.e.: / mode / tag)

2.RegExp constructor, which is used as follows:

New RegExp ("pattern" [, "flags"]) (that is, new RegExp ("mode" [, "tag"]))

Parameters:

Pattern (pattern): text representing a regular expression

Flags (tag): if specified, flags can be one of the following:

G:global match (fully determined match)

I:ignore case (ignore case)

Gi:both global match and ignore case (matches all possible values and ignores case)

Note: the parameters in the text format should not be marked with quotation marks, while the parameters of the second generator function should be marked with quotation marks. So the following expression

Is equivalent:

/ ab+c/i = new RegExp ("ab+c", "I")

Description:

When using the constructor function to create a regular expression, it is necessary to use the normal string to bypass the rules (adding the leading character\ to the string).

For example, the following two statements are equivalent:

Re=new RegExp ("\\ w +")

Re=/\ walled /

Note: RegExp presets the $attribute

$1,., $9 attribute

A string of matches enclosed in parentheses, if any.

Is a property of RegExp

Static, read-only

Available in JavaScript 1.2, NES 3.0 and above

Description: because input is a static property, not a property of individual regular expression objects. You can use RegExp.input to access the

Property.

There is no limit to the number of substrings that can be parenthesized, but regular expression objects can only retain the last nine. If you want to visit all the

For the matching string in parentheses, you can use the returned array.

New Document var regexp = new RegExp ("(\\ w +)\\ s (\\ w +)"); str= "John Smith"; newstr=str.replace (regexp, "$2"); newstr2=str.replace (regexp, "$1"); [xss_clean] ("original string:" + str+ "); [xss_clean] (newstr+"); [xss_clean] (newstr2+"); [xss_clean] ('$1 million subscription RegExp. 1 +" $2 = "+ RegExp.$2)

Second: the match () method can retrieve the specified value within a string or find a match of one or more regular expressions. It returns the specified value, not the position of the string.

Grammar

StringObject.match (searchvalue)

Description of stringObject.match (regexp) parameters

Searchvalue is required. Specifies the string value to retrieve.

Regexp is required. The RegExp object that specifies the pattern to match. If the parameter is not a RegExp object, you need to first pass it to the RegExp constructor to convert it to a RegExp object.

Return value

An array that holds the matching results. The contents of the array depend on whether the regexp has the global flag g.

Description

The match () method retrieves the string stringObject to find one or more text that matches the regexp. The behavior of this method depends to a large extent on whether regexp has the flag g.

If regexp does not have a flag g, then the match () method can only perform a match once in the stringObject. If no matching text is found, match () returns null. Otherwise, it returns an array of information about the matching text it finds.

Examples of match usage:

New Document var str= "1 plus 2 equal 3"; var str2= "11-23-55"; var results=str.match (new RegExp ("\\ d +", "gi")); for (var iTuno)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 302

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report