Rino Raj
Rino Raj

Reputation: 6264

How to get the string inside single quotes?

I have a sample string like http://ezsearch.annuitynexus.com/javascript:popOne('http://www.genworth.com/%20')

from this I need to get http://www.genworth.com/%20

Regex I used was /'(.*?)'/

The code I tried was

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

But its returning an empty array. What am I missing?

FIDDLE in which I tried the RegEx

Upvotes: 3

Views: 110

Answers (4)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627370

You need not add the regex delimiters /.../ in coldfusion regex. It is also advisable to turn the lazy dot matching to a negated character class so that the regex becomes more efficient.

Use

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

Note that [^'] matches any character other than a single apostrophe.

The match is inside the capture group 1.

See regex demo

Upvotes: 0

Sev Roberts
Sev Roberts

Reputation: 1295

Since this is a Coldfusion question I will offer a Coldfusion answer without overcomplicating it with a RegEx :-)

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

Upvotes: 2

Rino Raj
Rino Raj

Reputation: 6264

Any way I found the solution. I dont think it is proper. But still it works.

<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#"> 

OR

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

works fine. But in this output is something like 'http://www.genworth.com/%20' so I need to remove first and last character

Upvotes: 0

angelcool.net
angelcool.net

Reputation: 2546

Escape your single quotes:

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/

Good luck!!

Upvotes: 0

Related Questions