如何在单引号内输入字符串?

时间:2015-11-13 18:03:23

标签: regex coldfusion

我有一个样本字符串 的 http://ezsearch.annuitynexus.com/javascript:popOne(' http://www.genworth.com/%20&#39)

从此我需要得到 的 http://www.genworth.com/%20

我使用的正则表达式是/'(.*?)'/

我试过的代码是

<cfset link = "http://ezsearch.annuitynexus.com/javascript:popOne('http://www.genworth.com/%20')">
<cfset matches = REMatch("/'(.*?)'/", link) /> 
<cfdump var="#matches#"> 

但它返回一个空数组。我错过了什么?

FIDDLE我尝试过RegEx

4 个答案:

答案 0 :(得分:2)

由于这是一个Coldfusion问题,我将提供Coldfusion答案,而不会使用RegEx过度复杂: - )

<cfset link = "http://ezsearch.annuitynexus.com/javascript:popOne('http://www.genworth.com/%20')" />
<cfset matches = ListGetAt(link, 2, "'") />
<cfdump var="#matches#" />

答案 1 :(得分:0)

逃避单引号:

var str = "http://ezsearch.annuitynexus.com/javascript:popOne('http://www.genworth.com/%20')"; 
var res = str.match(/\'(.*?)\'/);
alert(res[1])

http://jsfiddle.net/s865b5bn/2/

祝你好运!!

答案 2 :(得分:0)

我找到解决方案的任何方式。我不认为这是正确的。但它仍然有效。

<cfset link = "http://ezsearch.annuitynexus.com/javascript:popOne('http://www.genworth.com/%20')">
<cfset matches = REMatch("'([^']*)", link) />
<cfset matches = Right(matches[1], Len(matches[1])-1) />
<cfdump var="#matches#"> 

<cfset link = "http://ezsearch.annuitynexus.com/javascript:popOne('http://www.genworth.com/%20')">
<cfset matches = REMatch("'(.*?)'", link) /> 
<cfdump var="#matches#"> 

工作正常。但是在这个输出中类似于'http://www.genworth.com/%20',所以我需要删除第一个和最后一个字符

答案 3 :(得分:0)

您无需在coldfusion正则表达式中添加正则表达式分隔符/.../。建议将惰性点匹配转换为否定的字符类,以使正则表达式更有效。

使用

<cfset matches = REMatch("'([^']*)", link) />

请注意[^']匹配除单个撇号之外的任何字符。

匹配位于捕获组1内。

请参阅regex demo