Jump to content

Move the word "The" from back to front


Jordanh

Recommended Posts

I need a rule that will format an entery that has ",the" at the end of it to say "The" first. Example: Field(Business)= "Cowboy Poetry, The" i need that entrey to display "The Cowboy Poetry"

 

I could sure use someone's help with this, i don't even know where to start.

Thank You

Link to comment
Share on other sites

There are lots of ways to do something like this, but using regular expressions is probably the most efficient:

var s = Field("Business");
//var s = "Cowboy Poetry, The";
return s.replace(/(.*)(?:\s*,\s*)(the)(\s*$)/gi, "$2 $1");

(That's some of my "Geeky Developer Poetry". ;))

 

Note that this can be generalized to handle other cases; for example:

 
var s = "Christmas Carol, A";
return s.replace(/(.*)(?:\s*,\s*)(the|a)(\s*$)/gi, "$2 $1");

Note the addition of |a in the second case.

 

P.S. Please don't cross-post threads to multiple forums.

Link to comment
Share on other sites

The rule worked great. Thank you so much. I was thinking as i put it into FP, what if the field had "Cowboy Peotry, The Amazing" in it? I found that your rule cut ", the Amazing" out and just added "The" to the front of it. I am not saying your rule doesn't work. I think you did a great job and i deeply appreciate your help. Is there a way to make the rule work for "Cowboy Poetry, The Amazing"?

 

Thanks again for getting back so fast.

Link to comment
Share on other sites

The rule worked great. Thank you so much. I was thinking as i put it into FP, what if the field had "Cowboy Peotry, The Amazing" in it? I found that your rule cut ", the Amazing" out and just added "The" to the front of it.

 

Are you sure? If I do this:

 
var s = "Cowboy Peotry, The Amazing";
return s.replace(/(.*)(?:\s*,\s*)(the|a)(\s*$)/gi, "$2 $1");

and click Validate, it returns:

 

Cowboy Peotry, The Amazing

 

Which makes sense because it doesn't match the regular expression. (There's something else besides whitespace (\s*) between the word "the" and the end of the string ($).)

 

So, my question to you is, did you change the regular expression from what I wrote? Also, can you please follow the suggestion in my signature and post what version of FusionPro you're using? (I'm basically using 5.8P1g on Windows.)

 

I am not saying your rule doesn't work. I think you did a great job and i deeply appreciate your help. Is there a way to make the rule work for "Cowboy Poetry, The Amazing"?

 

Well, like I said, it doesn't seem to be doing the same thing for you as it's doing for me. But you're also changing the requirements here. In other words, you're asking me to solve a different problem now than you were in your original post. I strongly advise you to go back and think about the requirements, that is, try to make a comprehensive list of all the circumstances under which you want to rearrange the text. The challenge in VDP is always to anticipate every possible input and how you want to handle it.

 

Anyway, try this:

 
return s.replace(/(.*)(?:\s*,\s*)([the|a].*$)/gi, "$2 $1");

Link to comment
Share on other sites

Oh i'm sorry, my version is 5.8P1b, i put the right one in my signature.

 

This code that you sent worked great for "Cowboy Poetry, The Amazing" as well as any other entrey with a word after ", the"

 

Code:

return s.replace(/(.*)(?:\s*,\s*)([the|a].*$)/gi, "$2 $1");

 

Thank you so much for your help.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...