OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Calling JavaScript from Basic: regular expressions

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Thu Jun 30, 2005 1:58 pm    Post subject: Calling JavaScript from Basic: regular expressions Reply with quote

With OO2.0 beta, JavaScript support has been added to OO. The JavaScript features concerning regular expressions are more elaborate and easier to handle than the UNO approach. For example, in UNO there is no way to address the () subexpressions in the replace term.

This shows (i) how to call JavaScript from BASIC and (ii) applies that to a regular expression replace function, applicable to BASIC strings.

The BASIC:
Code:
Sub Main
'use this, if you install the JavaScript subroutine in My Macros.
'Then the Javascript routine is available in all documents
'
oMasterScriptProviderFactory = createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")
oMasterScriptProvider = oMasterScriptProviderFactory.createScriptProvider("")
oScriptReplace = oMasterScriptProvider.getScript("vnd.sun.star.script:Tools.Replace.js?language=JavaScript&location=user")

'use this, if you install the JavaScript locally in the current doc
'
'oDocScriptProvider = ThisComponent.getScriptProvider()
'oScriptReplace = oDocScriptProvider.getScript("vnd.sun.star.script:Library1.Replace.js?language=JavaScript&location=document")

sReturn = RegExpReplace(oScriptReplace, "127,0,0,1", "([0-9]{1,3}),([0-9]{1,3}),([0-9]{1,3}),([0-9]{1,3})", "gi", "$1 A $2 B $3 C $4")

msgbox sReturn
end sub

function RegExpReplace(oScriptReplace as Object, sSource as String, sRegExp as String, sGlobUpcase as String, sReplace as String) as String
RegExpReplace = oScriptReplace.invoke(Array(sSource, sRegExp, sGlobUpcase, sReplace ), Array(), Array())
end function


You have to install the following routine in JavaScript / My Macros in a Library "Tools" under the name Replace.js
Code:
sSource     = String(ARGUMENTS[0])
sRegExp     = String(ARGUMENTS[1])
sGlobUpcase = String(ARGUMENTS[2])
sReplace    = String(ARGUMENTS[3])

myRe   = new RegExp(sRegExp, sGlobUpcase)

ARGUMENTS[0] = sSource.replace(myRe, sReplace)
Back to top
View user's profile Send private message
Cybb20
Super User
Super User


Joined: 02 Mar 2004
Posts: 1569
Location: Frankfurt, Germany

PostPosted: Fri Jul 01, 2005 6:58 am    Post subject: Reply with quote

Hi there, nice example but things still seem to be pretty cryptic.
What do you want to do here: sReturn = RegExpReplace(oScriptReplace, "127,0,0,1", "([0-9]{1,3}),([0-9]{1,3}),([0-9]{1,3}),([0-9]{1,3})", "gi", "$1 A $2 B $3 C $4") ?

Looks ugly, doesn't it? :/

Christian
_________________
- Knowledge is Power -
Back to top
View user's profile Send private message Send e-mail
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Fri Jul 01, 2005 7:20 am    Post subject: Reply with quote

looks ugly, indeed. But regular expressions pay for their strength by ugliness ...

I admit, I have not shown a particular useful example, but one example, which demos a Javascript regexp capability, which UNO regexp does not have: The usage of subexpressions, marked by (...), in the replace term (the $s).
So
"127,0,0,1"
is expected to transform to
"127 A 0 B 0 C 1"

You cant do that by UNO regexp to the best of my knowledge.
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Wed Jul 06, 2005 8:25 am    Post subject: Reply with quote

The key useful technique I see is how to call a function written in another scripting language, passing parameters, and returning a result.

The Regular Expressions example is but one useful application of this technique.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Thu Jul 07, 2005 7:01 am    Post subject: Reply with quote

for an application of the very powerful JavaScript sort function in BASIC routines see http://www.oooforum.org/forum/viewtopic.phtml?p=84338#84338 . This also deals with the passing back of arrays from JavaScript routines
Back to top
View user's profile Send private message
tulipa
Newbie
Newbie


Joined: 06 Dec 2008
Posts: 1

PostPosted: Wed Dec 10, 2008 5:26 am    Post subject: Reply with quote

How do you get rid of JavaScript worms on your PC? Apparently I was infected with JavaScript worms, and I can't go into some forums without getting banned from them for "posting" them! How can I get rid of them?
_________________
what is javascript
Back to top
View user's profile Send private message
xavic69
Newbie
Newbie


Joined: 15 Mar 2009
Posts: 3

PostPosted: Wed Mar 25, 2009 1:19 pm    Post subject: Reply with quote

Quote:

Code:

sSource     = String(ARGUMENTS[0])
sRegExp     = String(ARGUMENTS[1])
sGlobUpcase = String(ARGUMENTS[2])
sReplace    = String(ARGUMENTS[3])

myRe   = new RegExp(sRegExp, sGlobUpcase)

ARGUMENTS[0] = sSource.replace(myRe, sReplace)


This JavaScript code gives me to Frame Script errors on OpenOffice 3.0:
1) "invalid flag after regular expression", and
2) "Cannot read property "0" from null"
Error #1 is corrected by not using the String casting, and error #2 using a returning variable different from ARGUMENTS[i], so
Code:

sSource     = String(ARGUMENTS[0])
sRegExp     = String(ARGUMENTS[1])
sGlobUpcase = ARGUMENTS[2])
sReplace    = String(ARGUMENTS[3])

myRe   = new RegExp(sRegExp, sGlobUpcase)
ret = sSource.replace(myRe, sReplace)

works fine.
Back to top
View user's profile Send private message
zottona
Newbie
Newbie


Joined: 14 Oct 2009
Posts: 1

PostPosted: Wed Oct 21, 2009 2:23 am    Post subject: Reply with quote

I'm not allowed to use spellcheck, cannot edit my questions, cannot press "show" on hidden (-5) answers. It says "must have Javascript enabled." I've gone to Tools, Internet Options, followed that whole thing. I've gone to Java.com and downloaded the latest version. Any other ideas?
__________________
keyword research ~ keyword tool ~ keyword tracking ~ affiliate elite
Back to top
View user's profile Send private message
wseverin
Newbie
Newbie


Joined: 29 Jul 2010
Posts: 1

PostPosted: Thu Jul 29, 2010 3:36 pm    Post subject: Reply with quote

With very minor tweaking this becomes a decent regex find/replace function for use with Calc. I'm going to include a bit more detail in this description for the benefit of folks who don't want to become experts; they just want to get some work done.

I'm here using OpenOffice 3.2, build 9483. If you're using a different version YMMV.

What we're going to do is create a function called RegRep that can be used just like any other function in a Calc sheet cell. It will be called with four string arguments:
    1) sSource — the text to be searched
    2) sRegExp — the regex search pattern
    3) sGlobUpcase — the search rules; "g" for global, "i" for case insensitive
    4) sReplace — the replacement string/pattern

Here's an example of its use to return the source string in A2 with all of the lower case vowels removed except where they are followed by a space or the end of the string:
Code:
=REGREP(A2,"[aeiou](?!( |$))","g","")

This requires two snippets of code; one in ooBasic and the other in javascript. The javascript is necessary because it has decent regex handling ability whereas ooBasic does not.

To create the ooBasic code, create a new basic module in My Macros and paste in the code. Step-by-step: First, create a new macros file. To do this, on the Calc tool bar select: Tools–>Macros–>Organize Dialogs... and select the Modules tab. Select 'New' and name the module whatever you like; I called mine RegReplace. Then make sure your new module is highlighted and click 'Edit'. Copy the code below and paste it into the editor window. Then close the window.
Code:
function RegRep (sSource as String, sRegExp as String, sGlobUpcase as String, sReplace as String) as String

oMasterScriptProviderFactory = createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")
oMasterScriptProvider = oMasterScriptProviderFactory.createScriptProvider("")
oScriptReplace = oMasterScriptProvider.getScript("vnd.sun.star.script:Tools.Replace.js?language=JavaScript&location=user")

RegRep =  oScriptReplace.invoke(Array(sSource, sRegExp, sGlobUpcase, sReplace ), Array(), Array())

end function

To create the necessary javascript code we do something similar (but pay attention to the differences). From the toolbar select: Tools–>Macros–>Organize Macros–>Javascript... In the dialog that appears make sure My Macros is highlighted and click 'Create...'. In the new dialog window type in the name of the library, in this case 'Tools'. (Note- if you decide to use different names for the library or the javascript source, make sure you also make appropriate changes to the definition of oScriptReplace back in the ooBasic code.) Close the library name dialog. On the javascript macros window click 'Create...'. Another window will pop up asking you to name the javascript macro. Call it 'Replace' and close the window. (The window dialog code will automatically append a '.js' to the name.) Make sure the name of your javascript source is highlighted in the Javascript Macros window and click 'Edit'. Delete everything in the window (there may be a "Hello World" example). Copy the code below and paste it into the editor window:
Code:
sSource     = String(ARGUMENTS[0]);
sRegExp     = String(ARGUMENTS[1]);
sGlobUpcase = ARGUMENTS[2];
sReplace    = String(ARGUMENTS[3]);

myRe   = new RegExp(sRegExp, sGlobUpcase);
ret = sSource.replace(myRe, sReplace);

Then from the toolbar File–>Save, then close the editor window.

Ready to Go!
Back to top
View user's profile Send private message
Gfx-Dzign
Newbie
Newbie


Joined: 17 Oct 2012
Posts: 1

PostPosted: Wed Oct 17, 2012 10:16 am    Post subject: Reply with quote

Hi, sorry to reply on a 2 year old thread but I'm stuck..

I'm pretty new to OO Basic but am trying to work out a script in which I can use RegExp through OO Basic.

It could be that in the current 3.4.1 version RegExp is possible directly in OO Basic - Please tell me? Wink

If it's not I'm trying to get this last example to work. I followed the step by step (I hope correctly) but when I'm trying to run an example I get the error 'BASIC runtime error - variable not defined' on this line:

oMasterScriptProviderFactory = createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")

which seems logical as it indeed is not defined in the example.. could someone please tell me what I'm missing?

Thanks a lot in advance for any help Smile

Edit: I forgot to mention I'm trying this in -Write-.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group