在jquery中转义反斜杠字符 [英] Escapting backslash character in jquery

查看:559
本文介绍了在jquery中转义反斜杠字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用'highlight textarea'jquery插件。我需要在jquery中传递正则表达式。但是jquery没有识别反斜杠字符,即使我使用另一个反斜杠来逃避它。除了反斜杠之外,其他所有特殊字符都能正常工作。



 $(' #demo4')。highlightTextarea({
words:[' \(。+ ?)}'],
debug: true
});





如果我使用下面的正则表达式来获取{}之间的字符串,它可以正常工作。

单词:['{(。+?)}'] 



但是为了得到\}之间的字符串,它不能识别这个\字符,即使是正则表达式也是

单词:['  \\(。+?)}]  





那么如何在这个正则表达式中使用反斜杠来识别。

任何帮助将不胜感激

解决方案

< blockquote>(' #demo4')。highlightTextarea({
words:[' \(。+?)}'],
debug: true
});





如果我使用下面的正则表达式来获取{}之间的字符串,它可以正常工作。

单词:['{(。+?)}'] 



但是为了得到\}之间的字符串,它不能识别这个\字符,甚至正则表达式是

单词:['  \\(。+?)}]  





那么如何在这个正则表达式中使用反斜杠来识别。

任何帮助都将非常感谢


反斜杠在字符串和正则表达式中有意义,所以你必须逃避它两次。



这个意味着您要匹配的每个反斜杠必须由四个反斜杠替换:

单词:[' < span class =code-string> \\\\(。+?)}'] 


I am using 'highlight textarea' jquery plugin. I need to pass a regex in jquery. But jquery not recognising backslash character even if I use another backslash to escape it.All other special characters works fine except backslash.

$('#demo4').highlightTextarea({
          words: ['\(.+?)}'],
          debug: true
      });



if I use below regex to get a string between {},it works fine.

words: ['{(.+?)}']

,
But to get a string between \},its not recognising this \ character, even the regex is

words: ['\\(.+?)}]



So how to use backslash to be recognised in this regex.
Any help will be greatly appreciated

解决方案

('#demo4').highlightTextarea({ words: ['\(.+?)}'], debug: true });



if I use below regex to get a string between {},it works fine.

words: ['{(.+?)}']

,
But to get a string between \},its not recognising this \ character, even the regex is

words: ['\\(.+?)}]



So how to use backslash to be recognised in this regex.
Any help will be greatly appreciated


The backslash has meaning in strings and in regular expressions, so you have to escape it twice.

This means that each backslash you want to match must be replaced by four backslashes:

words: ['\\\\(.+?)}']


这篇关于在jquery中转义反斜杠字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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