Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 457
  • 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

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!

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