0

I have a string "@{Name; [email protected]}"

I want to write a regular expression that extracts Name and 11112121 from the above string

This is what I tried.

function formatName(text){ 
  var regex = /@\{([^;]+); ([^\}]+)\}/
  return text.replace(
      regex,
      '$1, $2'
  );  
}

The above gives Name, [email protected]. But I want only Name, 11112121

3 Answers 3

3

try this

var regex = /@\{([^;]+); ([^\}]+)(@.*)\}/

$1 => Name

$2=> 11112121

Here is the working example

Sign up to request clarification or add additional context in comments.

Comments

2

Use match instead, like this:

var regex = /@\{([^;]+);\s+([^@]+)/;

var matches = text.match(regex);

alert(matches[1] + ', ' + matches[2]);

http://jsfiddle.net/rooseve/bM2U6/

Comments

0

If you want to match everything until the @ character, you can use

var regex = /@\{([^;]+); ([^@]+)/

If you need to verify that the string also contains a } after that, you can add that to the end:

var regex = /@\{([^;]+); ([^@]+)[^}]*\}/

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.