1

How to convert string to regex pattern output? For example, var num ='1234567890' number is fixed 10 digits regex pattern changes. *1* ^[0-9]{3}-[0-9]{3}-[0-9]{4}( x[0-9]{1,5})?$ 2.^([0-9]{3}) [0-9]{3}-[0-9]{4}$ 3.... so on up to n.

Output should be based on the pattern given for Pattern 1 output 123-456-7890

Pattern 2 output (123) 456-7890 . . pattern n output .... is it possible to give regex pattern and string get excepted output

var result = num.replace(pattern,inputstring)
4
  • Already asked and answered: stackoverflow.com/questions/8760070/…. If this does not answer your question, you need to be more specific in exactly what format you are trying to accomplish from what type of text, not just an "in general" question like you have. Commented Dec 29, 2015 at 16:44
  • in above link pattern is already know but in my case regx pattern is getting from api response so i dont known which pattern comes Commented Dec 29, 2015 at 16:54
  • You need to ask your question better than because it's not clear, which is why you don't have anyone answering you: stackoverflow.com/help/how-to-ask Commented Dec 29, 2015 at 17:09
  • are you just trying to handle someone entering phone numbers in different ways and consolidating it to a single format??? You aren't saying where you are getting the inputs from, how many there are, and what different types of input you might have. Commented Dec 29, 2015 at 17:56

1 Answer 1

2

To do a replace from a webservice, you need both the regex pattern and the replace pattern from the web service. If you don't get both of these, your code is clueless.

var formatted,
    num = '1234567890'
;
$.get('webservice',function(result) {
    var regex     = new RegExp(result.Regex),
        replace   = result.ReplacePattern
    ;
    formatted = num.replace(regex, replace);
}
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.