By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
Responding to and Tracking Key Presses
How to Work With Responding to and Tracking Key Presses
JavaScript
Passing a JavaScript Value Between HTML Pages
Passing a JavaScript Value Between HTML Pages
JavaScript
Compare Objects in an Array
JavaScript Problem: Compare Objects in an Array
JavaScript
Switching Name Order Using Capturing Groups in Regular Expressions
Switching Name Order Using Capturing Groups in Regular Expressions
JavaScript
Shuffling an Array
JavaScript Problem: How to Perform Shuffling an Array
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Switching Name Order Using Capturing Groups in Regular Expressions
JavaScript

Switching Name Order Using Capturing Groups in Regular Expressions

Admin
Last updated: 2023/01/29 at 5:22 AM
Admin
Share
3 Min Read
Switching Name Order Using Capturing Groups in Regular Expressions

In this article, we are going to see how we to do Switching Name Order Using Capturing Groups in Regular Expressions. So we will have an array of name where we have some values, and we will use a regular expression to exchange their positions. There are multiple possibilities to make this thing happen, but we are going to see a very good method to get optimal solution.

Contents
Solution:You may also like:

Solution:

Okay now lets see the code solution, Here we created an array with some name, and we added a comma and space between these last names and first names. Here we are aiming to reverse this name in order of first name and last name. Then we applied map() method on this array, also we have added a callback function with name as parameter. As we know, map() is used to iterate through the array, and this will work as a loop. So in name parameter, all name strings pass through the name parameter.

Now we need to apply some logic on these names, so that we can get switched values in string. For that we used replace method where we applied a regular expression as a first parameter. To write regular expression, we will use // and in between this (\w+), (\w+). First (\w+) will target first string’s word, and we used , (\w+) to target second word, we need to apply comma and space in regular expression. We need to apply the same spaces and characters, so we can get all words. This thing known as capturing words.

Now after getting both words, we have added a second parameter, where we used $2 which will target second word then we added $1 which targets first word. This parameter works for switching parameter.


let namesArr = ["Doe, John", "Yeager, Eren", "Hales, Alex", "Peterson, Kevin"];

let newArr = namesArr.map((name) =>{
       return name.replace(/(\w+), (\w+)/,"$2 $1");

});
console.log(newArr);
Switching Name Order Using Capturing Groups in Regular Expressions

You may also like:

  • How to Define a Class With Properties and Methods in JavaScript?
  • How to Implement Class Inheritance in JavaScript?
  • How to Find Duplicate Elements in a Given Array?

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

TAGGED: javascript, Switching Name Order Using Capturing Groups in Regular Expressions
Share this Article
Facebook Twitter Email Print
What do you think?
Love1
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Posted by Admin
Follow:
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article Shuffling an Array JavaScript Problem: How to Perform Shuffling an Array
Next Article Compare Objects in an Array JavaScript Problem: Compare Objects in an Array
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

Responding to and Tracking Key Presses

How to Work With Responding to and Tracking Key Presses

February 5, 2023
Passing a JavaScript Value Between HTML Pages

Passing a JavaScript Value Between HTML Pages

February 3, 2023
Compare Objects in an Array

JavaScript Problem: Compare Objects in an Array

January 30, 2023
Shuffling an Array

JavaScript Problem: How to Perform Shuffling an Array

January 27, 2023
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?