如何使用jquery将字符串中的所有空格字符放入数组中 [英] How to get all spacial characters in a string in to an array using jquery

查看:176
本文介绍了如何使用jquery将字符串中的所有空格字符放入数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



任何人都可以为我提供一个解决方案,将所有特殊字符添加到阵列中。



例如:



var description ='〜Special&角色è*';



我需要以下数组

Hello,

Can any one please provide me a solution to add all special characters into array.

For eg:

var description='~Special & Characters è *';

I need the below array

var specialCharacters= ["~", "&", "è",'*'];





我的尝试:



以下是我用C#实现的方式:





What I have tried:

Below is the way i implemented in C# :

string entityName ="abc def & * ()@";		   
		    entityName=entityName.ToStr().Replace(" ", "");
            var allowablespecialCharacters = @"#$%^*";
            string patterns = @"[^A-Za-z0-9" + specialCharacters + "]";
            var b = Regex.Matches(entityName, patterns, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
            return b.Count > 0 ? true : false;





我在客户端需要的东西。



The same thing i need in Client Side.

推荐答案

%^ *;
string patterns = @[^ A-Za-z0-9+ specialCharacters +];
var b = Regex.Matches(entityName,patterns,RegexOptions .IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
返回b.Count> 0?true:false;
%^*"; string patterns = @"[^A-Za-z0-9" + specialCharacters + "]"; var b = Regex.Matches(entityName, patterns, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled); return b.Count > 0 ? true : false;





我在客户端需要同样的东西。



The same thing i need in Client Side.


虽然给你答案总是比较容易,但我们CP更喜欢更难的方式,因为这是帮助你的真正方法。 br />
首先,用空字符串替换字符串中的非特殊字符使用

JavaScript RegExp参考 [ ^ ]和< a href =https://www.w3schools.com/jsref/jsref_replace .asp> JavaScript String replace()方法 [ ^ ],然后使用 JavaScript String split()方法 [ ^ ]
Although it's always easier to just give you the answer, we in CP prefer the harder way because that is the real way to help you.
First, replace non-special characters in the string with empty string using
JavaScript RegExp Reference[^] and JavaScript String replace() Method[^], then make them into an array using JavaScript String split() Method[^]


这篇关于如何使用jquery将字符串中的所有空格字符放入数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆