Hi Everyone.
Just a quick question; Can I search for multiple instances of a word from an action and replace that word.
What I would like to do is have an action check to see if “for example” my html contains the word PEMS and then change all the instances of PEMS within the html to say HELEN
is this possible to do in one.
I am assuming the action will have to run at
function fwAfterEndHTML(){
and the action would contain some sort of
MyText.replace(/PEMS/g,'HELEN');
but would this just find the first instance of the word PEMS
Any guidance would be very appreciated, as I desperately try to get to grips with searching and replacing items within actions.
Just make sure that your text object has the entire run of text in it, and your regular expression looks like it should work just fine. The /g modifier means greedy, so it will work until there are no more matches for your pattern.
Hi Pems
as well as the g (global) modifier you can also use i so the patten checked is case insensitive so in your mini example you could do this:
function fwBeforeEndBody()
{
var bodyTag = fwDocument.fwTags.fwFind("body");
enclosing = bodyTag.fwEnclosing;
var MyText = bodyTag.fwToHTML();
// use g modifier to find all PEMS within the body
// use gi if you also want the search and replace to be case insensitive
MyText = MyText.replace(/PEMS/gi,'HELEN');
enclosing.fwAddRaw(bodyTag,MyText);
bodyTag.fwDelete();
}
You wouldn’t have to run at end of html you could invoke the function at end of the body
anyway thats how I would do it though other may have a better way
Hi Max,
Your code will work flawlessly although you should consider running
this at fwAfterEndHTML (the last point you can interact with the
document) as writing the body content back as a string will break any
action that attempts to do anything with this content later in the
publishing cycle. Although actions should always check to see if a tag
is available before working on it to prevent any errors to the user
the action may just seem not to be doing anything.
I’d also recommend swapping the body tag for a fwFindAll on a
paragraph in the hope of keeping the string replacement down to as few
lines as possible.
Regards,
Tim.
On 17 Dec 2009, at 07:50, max wrote:
Hi Pems
as well as the g (global) modifier you can also use i so the patten
checked is case insensitive so in your mini example you could do this:
function fwBeforeEndBody()
{
var bodyTag = fwDocument.fwTags.fwFind(“body”);
enclosing = bodyTag.fwEnclosing;
var MyText = bodyTag.fwToHTML();
// use g modifier to find all PEMS within the body
// use gi if you also want the search and replace to be case
insensitive
MyText = MyText.replace(/PEMS/gi,‘HELEN’);
enclosing.fwAddRaw(bodyTag,MyText);
bodyTag.fwDelete();
}
You wouldn’t have to run at end of html you could invoke the
function at end of the body
anyway thats how I would do it though other may have a better way
ahahahhh yes tim absolutely right… :o)
Pems ignore my bit about running it early as Tim’s seen the big flaw in my script… and yes rather than the whole body tag which means the lot being converted using just the paragraph would be a much better idea.
Tim, isn’t there a tag you can use to do string replacements while
keeping the document in the Freeway Object realm? I thought there was
a trick for that.
Would you need to loop through all the tags, find their content with
fwFindRaw and then update the content of each tag with fwAddRaw,
fwDelete?
Walter
On Dec 17, 2009, at 8:45 AM, max wrote:
ahahahhh yes tim absolutely right… :o)
Pems ignore my bit about running it early as Tim’s seen the big flaw
in my script… and yes rather than the whole body tag which means
the lot being converted using just the paragraph would be a much
better idea.
Hi Tim Walter and Maxi a big thank you for your time.
After reading the different posts I have adjusted the script
function fwAfterEndBody()
{
var bodyTag = fwDocument.fwTags.fwFind("body");
enclosing = bodyTag.fwEnclosing;
var MyText = bodyTag.fwToHTML();
// use g modifier to find all PEMS within the body
// use gi if you also want the search and replace to be case insensitive
MyText = MyText.replace(/PEMS/gi,'HELEN');
enclosing.fwAddRaw(bodyTag,MyText);
bodyTag.fwDelete();
}
which works and I am probably being dim here but just replacing the “body” for p doesn’t work so how do you search within every paragraph within the html.
I have only had elementary success in finding items so is there a special way to search within multiple items on the page.
Walter if it is better to not convert to a string can I find out how to do this, some where within the manual.
I know this comes back to me and my very limited knowledge of how to manipulate the freeway code and I do apologies for my lack of knowledge but I will try to refrain from asking to many basic questions.
Thanks in advance for putting me on the correct course… honestly I am progressing.
Pems
Hi Pems,
Here’s a boilerplate action that will do a simple find and replace for
you. It uses fwFindRaw to locate the text so does away with converting
the body tag to a string and the problems that brings to other actions; http://www.freewayactions.com/code/find-and-replace.php
Thanks to Walter for making me look at this.
To answer our initial question, yes the thing you were looking for
(the body tag) would become an array of tags (p tags) and you would
then need to step through these doing the finding and replacing.
Regards,
Tim.
On 17 Dec 2009, at 14:58, pems wrote:
Hi Tim Walter and Maxi a big thank you for your time.
After reading the different posts I have adjusted the script
function fwAfterEndBody()
{
var bodyTag = fwDocument.fwTags.fwFind("body");
enclosing = bodyTag.fwEnclosing;
var MyText = bodyTag.fwToHTML();
// use g modifier to find all PEMS within the body
// use gi if you also want the search and replace to be
case insensitive
MyText = MyText.replace(/PEMS/gi,‘HELEN’);
enclosing.fwAddRaw(bodyTag,MyText);
bodyTag.fwDelete();
}
which works and I am probably being dim here but just replacing the
“body” for p doesn’t work so how do you search within every
paragraph within the html.
I have only had elementary success in finding items so is there a
special way to search within multiple items on the page.
Walter if it is better to not convert to a string can I find out how
to do this, some where within the manual.
I know this comes back to me and my very limited knowledge of how to
manipulate the freeway code and I do apologies for my lack of
knowledge but I will try to refrain from asking to many basic
questions.
Thanks in advance for putting me on the correct course… honestly I
am progressing.
Pems
Cool, Tim! You might want to expand your ‘where’ a bit, perhaps:
p|a|li|dd|dt|h1|h2|h3|h4|h5|h6
Can you think of any other text containers that Freeway is likely to
make?
Also, what are you using for your source code view – that’s really
nice!
Walter
On Dec 17, 2009, at 10:16 AM, Tim Plumb wrote:
Hi Pems,
Here’s a boilerplate action that will do a simple find and replace
for you. It uses fwFindRaw to locate the text so does away with
converting the body tag to a string and the problems that brings to
other actions; http://www.freewayactions.com/code/find-and-replace.php
Thanks to Walter for making me look at this.
To answer our initial question, yes the thing you were looking for
(the body tag) would become an array of tags (p tags) and you would
then need to step through these doing the finding and replacing.
Regards,
Tim.
On 17 Dec 2009, at 14:58, pems wrote:
Hi Tim Walter and Maxi a big thank you for your time.
After reading the different posts I have adjusted the script
function fwAfterEndBody()
{
var bodyTag = fwDocument.fwTags.fwFind("body");
enclosing = bodyTag.fwEnclosing;
var MyText = bodyTag.fwToHTML();
// use g modifier to find all PEMS within the body
// use gi if you also want the search and replace to be
case insensitive
MyText = MyText.replace(/PEMS/gi,‘HELEN’);
enclosing.fwAddRaw(bodyTag,MyText);
bodyTag.fwDelete();
}
which works and I am probably being dim here but just replacing the
“body” for p doesn’t work so how do you search within every
paragraph within the html.
I have only had elementary success in finding items so is there a
special way to search within multiple items on the page.
Walter if it is better to not convert to a string can I find out
how to do this, some where within the manual.
I know this comes back to me and my very limited knowledge of how
to manipulate the freeway code and I do apologies for my lack of
knowledge but I will try to refrain from asking to many basic
questions.
Thanks in advance for putting me on the correct course… honestly
I am progressing.
Pems
I need to build in a directory lister for this so it’s not hard coded
into the php and will simply ‘code view’ any text file I place in the
directory.
A project for a quiet afternoon I suspect - whenever they happen.
Regards,
Tim.
Hi Tim
Just been thinking about what you have done and its uses and I tell you where this would be an absolute advantage is when you needed to change certain characters into either thier numeric or name entities.
It would be fantastic for that.