简体   繁体   English

用于匹配url中特殊字符的正则表达式模式

[英]Regex pattern for matching special characters in url

I am trying to match url in javascript using match() function, It takes regex as a pattern. 我试图使用match()函数在javascript中匹配url,它将正则表达式作为模式。 I need to match special characters of url ( ://..:/.?=& ) 我需要匹配url的特殊字符( ://..:/.?=&

var url = 'http://www.sun.com:80/index.html?q=1&q2=2#fragment'; 

I have checked other pages but unable to find specific regex. 我检查了其他页面但无法找到特定的正则表达式。 and please also suggest good source to learn about writing regex. 并且还请建议学习写正则表达式的好资料。

[Update] I am trying to break url in strings save into the array like ( string= ['http', '', '', 'www', 'sun', 'com', '80', 'index', 'html', 'q', '1', 'q2', '2'] ) . [更新]我试图打破字符串中的url保存到数组中( string= ['http', '', '', 'www', 'sun', 'com', '80', 'index', 'html', 'q', '1', 'q2', '2'] )。 For example - if pattern matches a variable saves the index position of url and then i use slice() function to extract the value ,my working url might be on different languages also (urdu etc) , i need to write regex for specific url symbols (:/.?=&# ) 例如 - 如果模式匹配变量保存url的索引位置然后我使用slice()函数来提取值,我的工作URL也可能在不同的语言(urdu等),我需要为特定的url符号写正则表达式(:/。?=&#)

This is the simplest one (works for any language), since we added only the special characters here. 这是最简单的(适用于任何语言),因为我们在这里只添加了特殊字符。

decodeURI(window.location.href).split(/[\/.?=&:#]+/g);

The following is for English:- 以下是英文: -

   var res = window.location.href.split(/[^A-Za-z0-9]+/g);

res will be the result array containing all text(param and values) except the special characters. res将是包含除特殊字符之外的所有文本(参数和值)的结果数组。 There are better ways to parse the URL, but that depends on what is required. 有更好的方法来解析URL,但这取决于所需的内容。 This solution is for what you exactly asked for. 此解决方案适用于您的确切要求。 If URL contains Urdu characters:- 如果网址包含乌尔都语字符: -

decodeURI(window.location.href).split(/[^A-Za-z0-9\u0600-\u06ff]+/g);

\؀-\\u6ff -> this is the unicode range of URDU characters. \\ u0600- \\ u6ff - >这是unicode范围的URDU字符。 Now added that too. 现在也补充说。 Note, we added "decodeURI", so that first the non-english characters will get decoded and then we do the search using regex. 注意,我们添加了“decodeURI”,因此首先非英语字符将被解码,然后我们使用正则表达式进行搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM