Skip to main content

Using Regular Expression / Regex In Javascript!

JavaScript Logo


Writing a simple function to extract matched strings using Regex in Javascript


Today i want to show how to extract string data using Regular Expression in Javascript.

A regular expression is an object that describes a pattern of characters.

Syntax for javascript :

 /pattern/modifiers;

First we can create a simple function that will process the string extraction.

example of string input will be something like this

<p>This is just a simple example input string</p>
(
    [message] => "Hello",
    [desc] => "its a handshake hello!"
)
<p>The purpose is to extract message and desc pattern matching</p>

Simple function that will process the string extraction

function extract(stringInput) {
 var regexp = /\[(message|desc)\](.*)/gm;
 var arr = [];
 while (result = regexp.exec(stringInput)) {
 arr.push(result[0]);
 }
 return arr; 
}

The 'extract' function will look for a pattern on anything that has opening bracket with the matching group either 'message' or 'desc'. And the pattern will include any words that are matching after the closing bracket!

'/g' is a flag modifier to let the regular expression to include matching pattern more than one match(all matches) and '/m' is a flag modifier to go through multi-lines of input string!

using result[0] to include all matching pattern not only strict to the result of group match only!

and because we are looking for all group matches, we need to iterate the result to get the next group match! else we will only get the first match result.

if we print the returned 'arr', we will get this output!

[message] => "Hello", [desc] => "its a handshake hello!"


if you are not familiar with regular expression, i'll suggest to read this guide first!


if you have any question, feel free to comment down below~ I'll try my best to help you out!

Comments

Popular posts from this blog

Tutorial on Min3D framework using Android Studio

Salam peeps, UPDATES***, the model on the old link is no longer working. so i have create a github repo, where i put the source code in a project, you guys can try to clone in and run on your android studio device emulator or directly on your phone, Ive replaced the model with a cube. https://github.com/aliaramli/Min3DTutorial Previously i ve posted tutorial on min3D using eclipse IDE, i believe most of us has moved to Android Studio IDE in developing android apps? As previous tutorial shows a lot of support from readers and among hot post in my blog, i ve decided to post the same tutorial but this time using Android Studio. For those who are familiar with Eclipse/Android Studio migration they might not have problem in running this tutorial . For more detail explanation on min3D please visit this website page Ok lets get started. Step One Create a new android project in android studio. you may name it as what you like, below are how i defined my project settings. ...

Get the last active time from users in woocommerce using sql query

To easily get the last active time from users (under Woocommerce extended plugin) we can easily query from database using the meta_key of "wc_last_active" example query: select user_id, meta_value from wp_usermeta where meta_key= "wc_last_active" and user_id in (11111,112222); results: +-------------+------------------+ | user_id    | meta_value | +-------------+-------------------+ |   11111   | 1556755200 | |   112222 | 1566518400 |

AWS Workspace Device is not Authorized Linux Client

Have you guys tried the AWS Workspace? They are offering the service for free from April - June 2020. For a limited time, in response to COVID-19 , starting April 1, 2020 through June 30, 2020, Amazon WorkSpaces Free Tier provides up to 50 WorkSpaces with Windows Standard bundle with 80 GB Root and 50 GB User Volumes, running in AutoStop mode for up to 10,000 hours of combined use per month across all AWS Regions where WorkSpaces Free Tier is currently available. Additionally, the Amazon WorkSpaces Free Tier provides one WorkSpace with Windows Performance bundle with 80 GB Root and 100 GB User Volumes, running in AutoStop mode for up to 200 hours, two WorkSpaces with Linux Standard bundle with 80 GB Root and 50 GB User Volumes, running in AutoStop mode for up to 400 hours, and one WorkSpace with Windows Value bundle with 80 GB Root and 10 GB User Volume, running in AutoStop mode for up to 200 hours. All new customers that start using WorkSpaces for the first time during ...