Summary of jquery's method of removing duplicate strings_jquery
本文实例总结了jquery实现去除重复字符串的方法。分享给大家供大家参考,具体如下:
这里总结了三种去除重复字符串的方法:
把例子贴上,用jQuery方便些,首先要搭好环境,就是在同一目录下(同一文件夹下)保证有所使用的jquery1.8.1(如果是其他版本就在html代码中作相应改动)
第一:
<html> <head> <script src="jquery-1.8.1.js"></script> <SCRIPT LANGUAGE="JavaScript"> $(function(){ $('#delRepeat').click(function(){ var str = $('#repeatValue').val(); var strArr=str.split("");//把字符串分割成一个数组 strArr.sort();//排序 var result=new Array();//创建出一个结果数组 var tempStr=""; for(var i in strArr) { if(strArr[i] != tempStr) { result.push(strArr[i]); tempStr=strArr[i]; } else { continue; } } $('#noRepeat').val(result.join(""))//把数组连成字符串并展示到页面 }) }) </SCRIPT> </head> <body> 原值<input id="repeatValue" type="text" ><input id="delRepeat" type="button" value="去重"> <input type="text" id="noRepeat"> </body> </html>
说明:通常就是把字符串分割成数组,再对数组操作,相对来说数组的方法多些,方便些,最后再join成字符串
关于sort()方法,之所以先对数组元素排序,就是因为可以把相同的字符归到一起,就不用再双层循环,要不然就得拿到每个元素,和剩余的逐个比对,这个sort会按照ASCII 字符顺序进行升序排列
第二:
<html> <head> <script src="jquery-1.8.1.js"></script> <SCRIPT LANGUAGE="JavaScript"> $(function(){ $('#delRepeat').click(function(){ var str = $('#repeatValue').val(); var strArr=str.split(""); //排序 strArr.sort(); var result =$.unique(strArr); $('#noRepeat').val(result.join("")); }) }) </SCRIPT> </head> <body> 原值<input id="repeatValue" type="text" ><input id="delRepeat" type="button" value="去重"> <input type="text" id="noRepeat"> </body> </html>
说明,这个unique方法确实方便,但有两个缺陷:
1、只对数组有效(直接字符串不行),并且该数组不能是数字数组,
2、只对相邻的重复元素有效,隔开的不行。
例如:[a,a,b,b,c,c]---unique----》[a,b,c]有效
[a,a,b,b,c,c,a]--unique-->[a,b,c,a]元素a仍然重复,无效
因此调用unique之前必须调用一下sort方法将其重复元素重排一下,挨在一起
但也因为调用了sort方法,顺序给重排了如:[b,b,c,c,a,a]---unique-->[a,b,c]不是[b,c,a]
第三:
<html> <head> <script src="jquery-1.8.1.js"></script> <SCRIPT LANGUAGE="JavaScript"> $(function(){ $('#delRepeat').click(function(){ var str = $('#repeatValue').val(); var reg = /(.)(?=.*\1)/g;//预搜索方式(有的叫断言) var result = str.replace(reg, ""); $('#noRepeat').val(result); }) }) </SCRIPT> </head> <body> 原值<input id="repeatValue" type="text" value="aca" ><input id="delRepeat" type="button" value="去重"> <input type="text" id="noRepeat"> </body> </html>
说明:
var reg =/(.)(?=.*\1)/g;
.匹配任意字符,但只能匹配任意字符中的一个
(.)加上()就是将匹配的该字符存储起来供以后引用
(?=)预搜索(也有叫断言的,也有叫预查的),指明某个字符的右侧是什么,但不包含这部分,只取这个‘某个字符'
如:p(?=ing) 匹配字符串ping时匹配成功,但匹配到的字符是p不是ping
(?=.*\1) 这个\1就是指的前面(.)的这个字符,之前说它被加上小括号就是被存储起来了,现在1就是取存储的第一个(共一个)
*匹配次数,也有人称之为量词,指出现任意次
.*指出现任意次任意字符
(.)(?=.*\1)指第一个匹配字符,如果右侧出现的内容中包含该字符时就匹配上该字符
g 指globle,全局匹配模式,匹配所有字符串
这个去重的结果其实是倒着来排序的,就是说重复字符出现在前面的都被置空了,是按一个字符从后往前出现的顺序排的
另:关于去重感兴趣的朋友还可以参考本站在线工具:
更多关于jQuery相关内容可查看本站专题:《jQuery操作xml技巧总结》、《jQuery拖拽特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》、《jquery选择器用法总结》及《jQuery常用插件及用法总结》
希望本文所述对大家jQuery程序设计有所帮助。

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Title: How to determine whether a string ends with a specific character in Golang. In the Go language, sometimes we need to determine whether a string ends with a specific character. This is very common when processing strings. This article will introduce how to use the Go language to implement this function, and provide code examples for your reference. First, let's take a look at how to determine whether a string ends with a specified character in Golang. The characters in a string in Golang can be obtained through indexing, and the length of the string can be

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

Go language is a powerful and flexible programming language that provides rich string processing functions, including string interception. In the Go language, we can use slices to intercept strings. Next, we will introduce in detail how to intercept strings in Go language, with specific code examples. 1. Use slicing to intercept a string. In the Go language, you can use slicing expressions to intercept a part of a string. The syntax of slice expression is as follows: slice:=str[start:end]where, s

How to check if a string starts with a specific character in Golang? When programming in Golang, you often encounter situations where you need to check whether a string begins with a specific character. To meet this requirement, we can use the functions provided by the strings package in Golang to achieve this. Next, we will introduce in detail how to use Golang to check whether a string starts with a specific character, with specific code examples. In Golang, we can use HasPrefix from the strings package

Methods to solve Chinese garbled characters when converting hexadecimal strings in PHP. In PHP programming, sometimes we encounter situations where we need to convert strings represented by hexadecimal into normal Chinese characters. However, in the process of this conversion, sometimes you will encounter the problem of Chinese garbled characters. This article will provide you with a method to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP, and give specific code examples. Use the hex2bin() function for hexadecimal conversion. PHP’s built-in hex2bin() function can convert 1

PHP String Matching Tips: Avoid Ambiguous Included Expressions In PHP development, string matching is a common task, usually used to find specific text content or to verify the format of input. However, sometimes we need to avoid using ambiguous inclusion expressions to ensure match accuracy. This article will introduce some techniques to avoid ambiguous inclusion expressions when doing string matching in PHP, and provide specific code examples. Use preg_match() function for exact matching In PHP, you can use preg_mat

PHP String Operation: A Practical Method to Effectively Remove Spaces In PHP development, you often encounter situations where you need to remove spaces from a string. Removing spaces can make the string cleaner and facilitate subsequent data processing and display. This article will introduce several effective and practical methods for removing spaces, and attach specific code examples. Method 1: Use the PHP built-in function trim(). The PHP built-in function trim() can remove spaces at both ends of the string (including spaces, tabs, newlines, etc.). It is very convenient and easy to use.
