0

I am trying to capture part of matched string. Here's what I've done:

var myRegex = /(^[0-9]{2}\.[0-9]{2}\.|$)/;
vary myString = "33.11.999";
var match = myRegex.exec(myString);
console.log(match[1]);

console will output 33.11. but i want to exclude the last dot. Is it possible to do that using regex?

1
  • Why do you need to match the end of string? What was your intention? Please add your pattern requirements. Commented Apr 13, 2017 at 14:27

3 Answers 3

1

You can use positive lookahead (?=), to ignore the last dot.

var myRegex = /(^[0-9]{2}\.[0-9]{2}(?=\.)|$)/;
var myString = "33.11.999";
var match = myRegex.exec(myString);
console.log(match[1]);

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

Comments

1

So why not capture only the part you want ?

var myRegex = /(^[0-9]{2}\.[0-9]{2})\.|$/;
var myString = "33.11.999";
var match = myRegex.exec(myString);
console.log(match[1]);

Comments

1

var regex=/\d+\.\d+(?=\.\d+)/;
var str='33.11.999';
var result=str.match(regex);
console.info(result[0]);

you can write like this too.

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.