• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 481
  • Last Modified:

JavaScript/REGEX: Every Third Character

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
hankknight
Asked:
hankknight
1 Solution
 
Robert SchuttSoftware EngineerCommented:
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
 
käµfm³d 👽Commented:
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
 
leakim971PluritechnicianCommented:
Test page : http://jsfiddle.net/7xbYp/
var str = 'HABEMULWLLAEOUP';
alert( str.replace(/(.)../g,"$1") );

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now