4 Replies - 3122 Views - Last Post: 06 June 2011 - 12:34 PM Rate Topic: -----

#1 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 195
  • Joined: 03-December 08

Regular Expressions multiple replace in one function

Posted 06 June 2011 - 07:06 AM

The Code:

<cfloop from="1" to="#thumbs.recordCount#" index="i" step="6">
<div>
<cfoutput query="thumbs" maxrows="6" startrow="#i#">
<img id="pics" src="#p2##name#" title="#rereplace(thumbs.name, 'AA[A-Z]?|_', ' ', 'ALL')#"  />
</cfoutput>
</div>
</cfloop>



I have a collection of images:
AAA_imageOne, AAB_imageTwo, AAC_imageThree, etc.

Above I have a regular expression that replaces a range, AA[A-Z], which prefix the filename of a bunch of images. For example: AAA_imageOne, AAB_imageTwo, AAC_imageThree, etc. become imageOne, imageTwo, imageThree, etc. The underbar in the image filename is also replaced.

Some of my image names have another character within them. Here's an example:

AAG_image-twelve

I would like to replace the dash (-) with something other than a blank space. So I can't group this character into the regex currently in use. Is there a way to combine both replace function into one? All at once I would like to replace the range (AA[A-Z]), with a blank space, the underbar (_) with a blank space, and the dash (-) with something OTHER THAN a blank space.

Here is what I tried, in an attempt to replace the dash with a slash:

<cfloop from="1" to="#thumbs.recordCount#" index="i" step="6">
<div>
<cfoutput query="thumbs" maxrows="6" startrow="#i#">
<img id="pics" src="#p2##name#" title="#rereplace(thumbs.name, 'AA[A-Z]?|_', ' ', 'ALL'|'-', '/', 'ALL')#"  />
</cfoutput>
</div>
</cfloop>



I'm not sure if this is even possible. Any ideas? Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Regular Expressions multiple replace in one function

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: Regular Expressions multiple replace in one function

Posted 06 June 2011 - 07:13 AM

try either consecutive replacements or a replace function.
var newName = thumbs.name.replace(/^\w{4}/, "").replace("-", "+");

Was This Post Helpful? 1
  • +
  • -

#3 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 195
  • Joined: 03-December 08

Re: Regular Expressions multiple replace in one function

Posted 06 June 2011 - 09:01 AM

View PostDormilich, on 06 June 2011 - 08:13 AM, said:

try either consecutive replacements or a replace function.
var newName = thumbs.name.replace(/^\w{4}/, "").replace("-", "+");


Here is my solution. Probably other ways to do it, but this one made sense to me. Thanks for the guidance.

<cfloop from="1" to="#thumbs.recordCount#" index="i" step="6">
<div>
<cfoutput query="thumbs" maxrows="6" startrow="#i#">
<cfset newName="#rereplace(thumbs.name, 'AA[A-Z]?|_', ' ', 'ALL')#">
<cfset newNamen="#rereplace(newName, '!', '/', 'ALL')#">
<img id="pics" src="#p2##name#" title="#newNamen#" />
</cfoutput>
</div>
</cfloop>


Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,614
  • Joined: 23-August 08

Re: Regular Expressions multiple replace in one function

Posted 06 June 2011 - 10:39 AM

This is ColdFusion, not Javascript.

Moved to ColdFusion
Was This Post Helpful? 0
  • +
  • -

#5 xheartonfire43x  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

Re: Regular Expressions multiple replace in one function

Posted 06 June 2011 - 12:34 PM

You can in fact do this with only one replace call. One of the awesome features of Regular Expressions is there support for variables. Basically everything wrapped in parentheses becomes a variable in the end. So you could use something like this to change all file names.

reReplaceNoCase(string,'(AA[A-Z]_)(image)(-)?([A-Za-z]*)','\2\4')


The two regex variables that I used are `\2` and `\4`. `\2` is the second matched rule (image) and `\4` is the fourth matched rule `([A-Za-z]*)`.

I tested that regex with: AAA_imageone and AAA_image-one and both returned imageone in the end.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1