cancel
Showing results for 
Search instead for 
Did you mean: 

Regular Expressions in Javascript API

randomman
Champ in-the-making
Champ in-the-making
Hi. Has anyone successfully used a regular expression in the Javascript API? Every time I try i seem to get an error.

The simplest attempt was a string replace method. e.g.
strBlah.replace(/expression/g, 'replace with me');
For some reason that never works but if I replace the expression with a string, it's fine. Also, if I try to use the RegExp object, it causes errors. It's almost as if RegExs haven't been implemented. Is this the case?

EDIT: Just to expand on this, I can use the example above in a web script but not in a standard script. i.e. it wont work if i put it in a js script which i then execute as an action.

Also, more script weirdness: I found that the string length property didn't seem to work properly when used on a node's content. I would get odd results. Outputting the value of the length would result in the source of a length function rather than a number. It was only solved by writing my own function to determine length which has to be my silliest bit of code.  :wink:

Is this a bug?
3 REPLIES 3

kevinr
Star Contributor
Star Contributor
>I can use the example above in a web script but not in a standard script
That's very strange. Both the webscript and standard javascript action implementation both use the same Rhino based ScriptService underneath! If Rhino supports it then so should we. If you use the RegExp object directly does it work? http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:RegExp

>Also, more script weirdness: I found that the string length property didn't seem to work properly when used on a node's content. I would get odd results.
Are you sure you are retrieving the length of the content itself or some property object? Remember the properties of type d:content are themselves objects which have further properties such as mimetype and size:
http://wiki.alfresco.com/wiki/JavaScript_API#ScriptContent_API
So you should be using:
node.properties.content.size

Thanks,

Kevin

randomman
Champ in-the-making
Champ in-the-making
Thanks for your response.

The root cause of my problem was indeed related to the content property of a ScriptNode. Although described as 'A read/write value that represents the content as a string', it doesn't actually behave like a js String.

In order to use String methods (such as a regex-based replace) on document content, I created a specific String object from the content. e.g.
var strContent = String(document.content);

Is it worth putting a note to that effect on the Wiki?

kevinr
Star Contributor
Star Contributor
Yes i think you are right. Rhino JavaScript generally is very good at converting Java String to JS String automatically - but for some operations (like getting a String out of a Map rather than directly from a getter method) it does not perform the conversion.

I will add a note about it.

Thanks,

Kevin