问题描述
我在JavaScript中使用regexp尝试测试字符串。
正确的字符串看起来像:
I try test string using regexp in JavaScript.Correct string looking like:
<script charset="utf-8">new DGWidgetLoader({"width":640,"height":600,"borderColor":"#a3a3a3","pos":{"lat":46.00650100065259,"lon":11.263732910156252,"zoom":9}
我想测试width,height看起来像xxx或xxxx,以及lat,lon
看起来像x {1,2} .x *,缩放看起来像x {1,2}
I want test that "width", "height" looks like xxx or xxxx, and "lat", "lon" looks like x{1,2}.x*, zoom looks like x{1,2}
我尝试使用这个正则表达式
I try use this regex
/<script charset="utf-8">new DGWidgetLoader(/{"width":[0-9]{3,4},"height":[0-9]{3,4},"borderColor":"#a3a3a3","pos":\{"lat":[0-9]{1,2}.[0-9]+,"lon":[0-9]{1,2}.[0-9]+,"zoom":[0-9][0-9]}//
使用String.search(),但收到错误 SyntaxError:无效的正则表达式:/< script charset =utf -8> new DGWidgetLoader(/ {width:[0-9] {3,4},height:[0-9] {3,4},borderColor:#a3a3a3, POS :{ LAT :[0-9] {1,2} [0-9] +, LON :[0-9] {1,2} [0-9] +, 变焦:[0-9] [0-9]} //:未终止的群组
with String.search(), but got error SyntaxError: Invalid regular expression: /<script charset="utf-8">new DGWidgetLoader(/{"width":[0-9]{3,4},"height":[0-9]{3,4},"borderColor":"#a3a3a3","pos":{"lat":[0-9]{1,2}.[0-9]+,"lon":[0-9]{1,2}.[0-9]+,"zoom":[0-9][0-9]}//: Unterminated group
我如何解析如下所示的脚本标记?
How can i parse script tag that looking like below?
推荐答案
你应该逃避(
, {
,}
和。
使用 \
:
You should escape (
, {
, }
and .
with \
:
/<script charset="utf-8">new DGWidgetLoader\(\{"width":[0-9]{3,4},"height":[0-9]{3,4},"borderColor":"#a3a3a3","pos":\{"lat":[0-9]{1,2}\.[0-9]+,"lon":[0-9]{1,2}\.[0-9]+,"zoom":[0-9][0-9]\}/
这篇关于正则表达式中未终止的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!