Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅将正则表达式应用于slug_Javascript_Regex_Workbox - Fatal编程技术网

Javascript 仅将正则表达式应用于slug

Javascript 仅将正则表达式应用于slug,javascript,regex,workbox,Javascript,Regex,Workbox,我正在使用下面的正则表达式来缓存我的服务人员中的所有html页面,该服务人员运行在一个使用相当长的永久链接的jekyll站点上(即排除“index.html”而使用simply/slug/): 在localhost:4000上运行时,这可以正常工作,但在生产环境中,域的“.com”部分会破坏这一点。我如何修改它以只在slug中查找匹配项 编辑:此处演示:在正则表达式的开头,使用(domain\.com | localhost:\d+)匹配域名或本地主机,然后匹配除点以外的任何字符序列(如您所做)

我正在使用下面的正则表达式来缓存我的服务人员中的所有html页面,该服务人员运行在一个使用相当长的永久链接的jekyll站点上(即排除“index.html”而使用simply/slug/):

在localhost:4000上运行时,这可以正常工作,但在生产环境中,域的“.com”部分会破坏这一点。我如何修改它以只在slug中查找匹配项


编辑:此处演示:

在正则表达式的开头,使用
(domain\.com | localhost:\d+
)匹配域名或本地主机,然后匹配除点以外的任何字符序列(如您所做)

(domain\.com | localhost:\d+
位转换为:

(            # start a group for capturing a match or selecting from alternate matches
domain\.com  # match the literal string "domain.com"
|            # or
localhost:   # match the literal string "localhost:"
\d+          # match a digit (\d) one or more times (+)
)            # end a group

另一个在生产和登台方面对我很有效的解决方案:

/\/[^.]+?$/

此处演示:

也许您想匹配可选的
…com
?试试
/^([^.]+\.com\/)?[^.]+$/
谢谢,Wiktor。试了一下,但似乎不起作用。理想情况下,正则表达式的功能应该足够多,可以在登台和生产条件下工作(即localhost:4000和domain.com)。我想你可以这样说:
/(?:^ yourdomain\.com\/)?[^.]+$/
谢谢,@Zak。出于某种原因,这似乎只适用于localhost:400,而不适用于domain.com。您可以在这里看到它的作用:我想最优雅的解决方案是让表达式匹配第一个
/
之后的任何内容,除非它包含
思想?@MLDR。要在regex101.com中多行测试正则表达式,您需要启用全局和多行标志。:)如。
(            # start a group for capturing a match or selecting from alternate matches
domain\.com  # match the literal string "domain.com"
|            # or
localhost:   # match the literal string "localhost:"
\d+          # match a digit (\d) one or more times (+)
)            # end a group
/\/[^.]+?$/