Solved

JavaScript/REGEX: Every Third Character

Posted on 2013-05-30
3
430 Views
Last Modified: 2013-05-30
Using JavaScript, how can I get every third character?

var string = 'HABEMULWLLAEOUP';
alert( string.match(/.{3}/g) ); // Should alert("HELLO):

Open in new window

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39208772
How about:
alert( string.replace(/(.).?.?/g, '$1') );

Open in new window

EDIT: The question marks are there in case the string is too short.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39208786
It is not possible to generate the output that you show above using a regex. You will need additional logic in order to generate the string that you show in your OP.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39208897
Test page : http://jsfiddle.net/7xbYp/
var str = 'HABEMULWLLAEOUP';
alert( str.replace(/(.)../g,"$1") );

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question