Solved

JavaScript/REGEX: Every Third Character

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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 invoke it on a web page. 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.: (CODE)

839 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