Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Web Tools Project (WTP) » Eclipse's Rhino JavaScript RegEx Not Working(Eclipse's Rhino JavaScript RegEx Not Working)
Eclipse's Rhino JavaScript RegEx Not Working [message #837487] Thu, 05 April 2012 18:32 Go to next message
Tonté  Pouncil is currently offline Tonté PouncilFriend
Messages: 13
Registered: July 2009
Junior Member
Hi,

I've encountered a problem while working with Eclipse WTP and JavaScript RegEx. It would seem Rhino does not work exactly the same as the web browsers do when it comes to regular expression. The code I am about to show runs perfectly on the web browser. However, it doesn't work so good when I run and debug inside Eclipse using the Rhino JavaScript. You can test it on the web at the Online Regular Expression Tester (www.pagecolumn.com/tool/regtest.htm) site. Okay here is the code that I ran and debug inside Eclipse using the Rhino JavaScript configuration:

//Compressed version of the string to search.
var compressedStr = "function TestClass(){\n\tthis.getName = function(){\n\t\treturn 'TestClass';\n\t};\n\n\tvar getType = function(){\n\t\treturn 'JavaScript';\n\t};\n\n\tfunction doTest(){\n\t\treturn 'doing test' ;\n\t};\n}";

var str = "function TestClass(){\n" +
	"\tthis.getName = function(){\n" + 
		"\t\treturn 'TestClass';\n" + 
	"\t};\n\n" + 

	"\tvar getType = function(){\n" + 
		"\t\treturn 'JavaScript';\n" + 
	"\t};\n\n" + 

	"\tfunction doTest(){\n" + 
		"\t\treturn 'doing test' ;\n" + 
	"\t};\n" + 
"}";
	
var re = new RegExp("this\.[a-zA-Z][a-zA-Z0-9]*\s*=\s*function", "g");
var testResult = re.test(str);
alert("testResult: " + testResult);


If you want to just copy and paste the function TestClass without all the newline and tab characters, use the follwoing as the test string at the Online Regular Expression Tester (www.pagecolumn.com/tool/regtest.htm) site:
function TestClass(){
	this.getName = function(){
		return "TestClass";
	};
	
	var getType = function(){
		return "JavaScript";
	};
	
	function doTest(){
		return "doing test";
	};
}


Here is the Regular Express Pattern to use at the Online Regular Expression Tester (www.pagecolumn.com/tool/regtest.htm) site:
this\.[a-zA-Z][a-zA-Z0-9]*\s*=\s*function


Here is the Regular Express Flag to use at the Online Regular Expression Tester (www.pagecolumn.com/tool/regtest.htm) site:
g


I was wonder if someone can help figure out why this test does not work when executed inside Eclipse Rhino JavaScript?

Thanks in advance!

Tonté
Re: Eclipse's Rhino JavaScript RegEx Not Working [message #838735 is a reply to message #837487] Sat, 07 April 2012 15:01 Go to previous message
Tonté  Pouncil is currently offline Tonté PouncilFriend
Messages: 13
Registered: July 2009
Junior Member
All,

I figured out what my problems was. It turns out that the white space in "\s" in JavaScript Regular Expression considers a whitespace to be more than just the whitespace character. It includes characters such as:

  • A tab character
  • A carriage return character
  • A new line character
  • A vertical tab character
  • A form feed character

And it turns out I need to only to search for the whitespace, only. In order to learn this, I had to use the unicode equivalent of the space character; which is [\u0020] in my regular expression. So I end up with the following revised regular expression:

this\.[a-zA-Z][a-zA-Z0-9]*[\u0020]+=[\u0020]+function[\u0020]*

I hope this tid-bit of information helps someone else.

Cheers!

Tonté

[Updated on: Sat, 07 April 2012 15:03]

Report message to a moderator

Previous Topic:Found character data inside an array element while deserializing
Next Topic:How to create a servlet 3.0 project with the web.xml file?
Goto Forum:
  


Current Time: Fri Nov 28 23:01:05 GMT 2014

Powered by FUDForum. Page generated in 0.03395 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software