I believe we all have used Regular Expression while working on different projects in different languages. Basically we Regular Expressions to match a pattern of characters. Here in this post we will see how you can define Regular Expression in Google Apps Script.
Importance of Regular Expression(RegEx) and why you should learn:
- RegEx helps you to shorten your code.
- Helps you in pattern matching to validate text input.
- It works with almost all programming language.
Here’s below code implementation of Regular Expression(RegEx) in Google Apps Script:
function RegularExp() { var string = "Hello World, Welcome to Weird Geek 2019"; var regExp = new RegExp("([a-z]+\\s[a-z]+\\,\\s[a-z]+\\s[a-z]+\\s[a-z]+\\s[a-z]+)","gi"); var lastName = regExp.exec(string)[1]; Logger.log(lastName); }
Basically in above code we have a string in Line 2 for which we have to define a RegEx and finally match it. In 3rd line we have define the RegEx where [a-z]+ is to matches a characters, \\s is for space. And finally we in line 4th we executed the pattern matching with the string via RegEx. Here, as a output we will get Hello World, Welcome to Weird Geek as we do not want the digit included in the link.
If you want the digits then just add [0-9]+ in the Regular Expression we defined in line 3rd line.
To get more detailed knowledge about Google Apps Script, you can check the official website. Also if you want to explore more codes/posts related to Google Apps Script on our website, then you can find it here.
If you have any questions or if you need any help please get in touch with me using the comment section below.
sudip modi says
This was useful. thanks!