用于匹配字符串开头和结尾的JavaScript正则表达式,并在匹配值中引用整个字符串?

时间:2017-07-03 10:23:39

标签: javascript regex

我在一个变量中有字符串

字符串

def foo(m):
    if m:
        return m.group(1).replace(' ', '')

string = "'In Absence of Good Men'"
new_string = re.sub("'(.*?)'", foo, string)

但我想要

{"index":"logstash-cdr","search_type":"count","ignore_unavailable":true}
{"query":{"filtered":{"query":{"bool":{"must":[{"query_string":{"query":"*","analyze_wildcard":true}},{"query_string":{"query":"*","analyze_wildcard":true}}]}},"filter":{"bool":{"must":[{"range":{"@timestamp":{"gte":1451586600000,"lte":1483208999000,"format":"epoch_millis"}}}],"must_not":[]}}}},"size":0,"aggs":{"2":{"terms":{"field":"PhoneNumber","size":100000,"order":{"_count":"desc"}}}}}
{"index":"logstash-cdr","ignore_unavailable":true}
{"highlight":{"pre_tags":["@kibana-highlighted-field@"],"post_tags":["@/kibana-highlighted-field@"],"fields":{"*":{}},"require_field_match":false,"fragment_size":2147483647},"query":{"filtered":{"query":{"bool":{"must":[{"query_string":{"query":"*","analyze_wildcard":true}},{"query_string":{"query":"*","analyze_wildcard":true}}]}},"filter":{"bool":{"must":[{"range":{"@timestamp":{"gte":1451586600000,"lte":1483208999000,"format":"epoch_millis"}}}],"must_not":[]}}}},"size":50,"sort":[{"@timestamp":{"order":"desc","unmapped_type":"boolean"}}],"fields":["*","_source"],"script_fields":{},"fielddata_fields":["@timestamp"]}
{"index":"logstash-fraud","search_type":"count","ignore_unavailable":true}
{"query":{"filtered":{"query":{"bool":{"must":[{"query_string":{"query":"*","analyze_wildcard":true}},{"query_string":{"query":"*","analyze_wildcard":true}}]}},"filter":{"bool":{"must":[{"range":{"@timestamp":{"gte":1451586600000,"lte":1483208999000,"format":"epoch_millis"}}}],"must_not":[]}}}},"size":0,"aggs":{"2":{"terms":{"field":"PhoneNumber","size":100000,"order":{"_count":"desc"}}}}}
{"index":"logstash-fraud","ignore_unavailable":true}
{"query":{"filtered":{"query":{"bool":{"must":[{"query_string":{"query":"*","analyze_wildcard":true}},{"query_string":{"query":"*","analyze_wildcard":true}}]}},"filter":{"bool":{"must":[{"range":{"@timestamp":{"gte":1451586600000,"lte":1483208999000,"format":"epoch_millis"}}}],"must_not":[]}}}},"highlight":{"pre_tags":["@kibana-highlighted-field@"],"post_tags":["@/kibana-highlighted-field@"],"fields":{"*":{}},"require_field_match":false,"fragment_size":2147483647},"size":50,"sort":[{"@timestamp":{"order":"desc","unmapped_type":"boolean"}}],"fields":["*","_source"],"script_fields":{},"fielddata_fields":["@timestamp"]}

要打印的这一行如何通过JavaScript实现相同的目标?

1 个答案:

答案 0 :(得分:0)

如果您将字符串存储在变量a中。您可以按如下方式使用substring()方法

var start =  a.indexOf('{"index":"logstash-fraud"');
c = a.substring(start,a.indexOf('{"index',start+1));