Home     |      Learn AJAX     |      Forum     |      Register     |      Submit Resource     |      Submit Article      |      Contact Us

Home AJAX-Tips
 

Regular Expression Compile

 

Data manipulation is very important in any programming language and it can be considered essential in an Ajax or JavaScript based application. Developers have to create functions so that data manipulation is not just possible but also automated. No one wants to create an application where it requires additional configuration after it's launched.


In this article we take a look at regular expression (RegExp) Compile. It's a smart JavaScript based function that can ease data manipulation for developers.


The compile function comes is written in this syntax:


object.compile(pattern[flags]}


Compile in RegExp is a function that will allow developers to add variables on data manipulation.


For example, a developer creates a function that extracts a certain word which should be replaced by another word. Developers can create a RegExp function to extract the word and creates a string that comes with the recommended replacement. The code should go this way:


myRegExp = /ben/
myString "Ben is not the smartest guy in the room"
newString = myString.replace(myRegExp, "Stan")


Running code will have an output wherein the word "Ben" will be replaced by Stan. But the developer is not done with the word "Ben" another word should be replaced by "Stan". This time, the word "Ruben" should also be replaced by Stan.


The compile code will go this way:


myRegExp.compile("(ru)?ben")
newSstring = myString.replace(myRegExp, "Stan")


With this code, the names Ben and Ruben will be replaced by stan. The function will look for these words and promptly replace them with the recommended word. But the "compile" function has additional flags. The default compile function will simply change the first data it encounters. The following flags can be added:


"g" - indicates that the every word should be replaced (global)
"i" - indicates that the search should be case insensitive.
"gi" - the combination of global and case insensitive commands.


The coding with the additional flags should go this way:


myRegExp.compile("word","g")


Warnings on Other Methods of RegExp


It is important to note that the compile function in RegExp can have some drastic effects in the data properties. The properties of other methods are replaced simply because the data has been changed by the compile method. To be specific, properties ignoreCase, global and source are changed. Developers have to check these properties before or after using the compile function. This is essential since the changed data should still work in the application.


Practical Uses of Compile Method


At first glance, compile is a simple data manipulation tool for data created in an Ajax or JavaScript based application. But the mere potential of compile will allow developers to improve their data without extensive editing. Since compile can be easily implemented in the entire document, additional settings are no longer needed.


But as indicated, it should be expected that the data value will be changed after compile is completed. It could be possible that the data will not longer readable by the application. It is important to check the application for bugs after this function to prevent bugs coming out of the application.



Read Next: Loading External Sources on jQuery



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links