2

I have a string which contains digits. I need to sort this string using regular expression.

var myString = "85762034834126745305743";

I'm looking for a complete solution which only use regular expression. Just need your thought on this whether it can be achieved or not.

7
  • Is there a reason you would like to use regex for this task? Commented Feb 26, 2014 at 20:32
  • 7
    Regular expressions cannot 'sort' a string. Commented Feb 26, 2014 at 20:32
  • @p.s.w.g So this can only be achieved with the help of a loop ? Commented Feb 26, 2014 at 20:34
  • 1
    Regular Expressions only match patterns they do not influence those matches. Commented Feb 26, 2014 at 20:34
  • 1
    Regular expressions allow you to check if a string matches a pattern, and possibly extract subsets of the string. Sorting means putting a list of items in a certain order. The two are unrelated concepts. Commented Feb 26, 2014 at 20:35

1 Answer 1

5

Regular expressions are not suited for this kind of task. Plain old JavaScript is a lot simpler and easier:

"85762034834126745305743".split("").sort().join("") // "00122333344445556677788"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.