Solved

JavaScript/REGEX: Every Third Character

Posted on 2013-05-30
3
424 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AJAX ModalPopupExtender will Not Hide in Asp.net 1 38
Coding for the first time 9 69
15 puzzle in Javascript/html 2 44
List the words in Notepad++ 16 14
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

733 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