简体   繁体   English

如何在从javascript中的json对象提取字符串时保留转义序列。 (字符串实际上是正则表达式模式)

[英]How to retain escape sequences while extracting strings from json objects in javascript. (Strings are actually regex patterns)

I have a JSON object as follows: 我有一个JSON对象,如下所示:

    var jsonObject = {"regex":"<span class=\"Value\">\\$(.+?)<\\/span>"};

My target is to use this regular expression to scrape a value from a html document. 我的目标是使用此正则表达式从html文档中抓取一个值。

    var match = html.match(new RegExp(jsonObject.regex, 'i'));

This however returns an error. 但是,这将返回错误。 The problem seems to be that the escape sequences in the regex string are lost in the string jsonObject.regex 问题似乎是正则表达式字符串中的转义序列在字符串jsonObject.regex中丢失了

A call to jsonObject.regex returns 调用jsonObject.regex返回

    < span class="Value">\$(.+?)<\ /span>

(The escape sequences like \\" and \\\\ are lost) (诸如\\“和\\\\之类的转义序列会丢失)

I could replace the respective characters using javascript, but it seems the inefficient thing to do since I already have the correct format in the json object. 我可以使用javascript替换各个字符,但是这似乎效率不高,因为我已经在json对象中使用了正确的格式。

Any clues or workarounds are appreciated. 任何线索或解决方法,不胜感激。 Thanks! 谢谢!

You are doing two things wrong here. 您在这里做错了两件事。

First and foremost, you are trying to build a program that uses arbitrary regular expressions on HTML. 首先,您要尝试构建一个在HTML上使用任意正则表达式的程序。 Don't do that. 不要那样做 You have a DOM at your disposal on the client side, you should use one of the selector engines available. 您可以在客户端使用DOM,应该使用可用的选择器引擎之一。 Examples include the browser built-in document.querySelectorAll() , Sizzle (which is also part of jQuery), NWMatcher , or an XPath-based selector engine like XPath.js . 示例包括浏览器内置的document.querySelectorAll()Sizzle (也是jQuery的一部分), NWMatcher或基于XPath的选择器引擎,如XPath.js

Then, you obviously do not use a JSON serializer to build your JSON string on the server side, or things like messed-up escaping would not happen on the client side. 然后,您显然不使用JSON序列化程序在服务器端构建JSON字符串,否则在客户端不会发生混乱的转义之类的事情。

Lastly, what you have in your first code sample is not JSON. 最后,第一个代码示例中的内容不是JSON。 It's a JavaScript object literal. 这是一个JavaScript对象文字。 JSON is always a string: JSON 始终是一个字符串:

'{"regex":"<span class=\"Value\">\\$(.+?)<\\/span>"}'

Selecting what you seem to want in jQuery would become as simple as 在jQuery中选择想要的内容将变得非常简单

var value = $("span.value").text();

But as I said, you are not bound to use jQuery, there are lighter-weight alternatives if HTML-scraping is your main goal. 但是正如我说的那样,您不一定要使用jQuery,但如果将HTML抓取作为主要目标,则可以使用轻量级的替代方法。

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

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