1

I've been searching but haven't found a solution.

I have a regexp:

var reg = /^((\d){1,5}(\.|,)(\d{2}))/;

And a string:

var str = '12323.098765421';

I'm trying to keep part that match regexp and delete all the rest.

What I'm doing:

str.replace(/^((\d){1,5}(\.|,)(\d{2}))/, '$1 // can't understand what should I put here to replace first part');
7
  • What is the expected output? 12323.09? See regex101.com/r/jF5tV6/1. But why remove? You can use RegExp#match: "12323.098765421".match(/^\d{1,5}[.,]\d{2}/)[0]. Commented Jun 17, 2016 at 10:12
  • didn't get can't understand what should I put here to replace first part part. Commented Jun 17, 2016 at 10:15
  • @DarthJS: Please clarify and let know if my examples work the way you expect. Commented Jun 17, 2016 at 10:25
  • is the goal to round to 2 places? Commented Jun 17, 2016 at 11:09
  • Wiktor Stribiżew if you want you can post your answer and I'll vote for it, 'couse you was first Commented Jun 17, 2016 at 11:14

3 Answers 3

1

Let's imagine you have a string that starts with a number that conforms to the pattern:

1 to 5 digits   +   decimal separator  +  2 digits

This pattern may be followed with any character, even including a newline. Then, in JS, you can use the following replacement:

.replace(/^(\d{1,5}[.,]\d{2})[\s\S]*/, "$1")

Where

  • ^ - matches the start of the string
  • (\d{1,5}[.,]\d{2}) - matches and captures our pattern (with , or . as decimal separators)
  • [\s\S]* - matches any 0+ characters, even including a newline.

var re = /^(\d{1,5}[.,]\d{2})[\s\S]*/; 
var str = '12323.098765421';
var result = str.replace(re, "$1");
console.log(result);

The $1 is a backreference to the value captured with Group 1 (formed with the help of a pair of unescaped parentheses).

See more information on capturing groups and backreferences.

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

Comments

1

The regex will match the required output, since you want to remove the rest then you need to update it like this.

var str = '12323.098765421';

console.log(
  str.replace(/^(\d{1,5}[.,]\d{2})\d+/, '$1')
);

Regex explanation here.


Or you need to use String#match method to get the matched string

var str = '12323.098765421';

console.log(
  str.match(/^\d{1,5}[.,]\d{2}/)[0]
);

Regex explanation here.

2 Comments

thanks for your reply. maybe I'm doing something wrong but, I'm testing in browser console and the result is something strange: var x = '12323.09876542'; x.replace(/^(\d){1,5}(\.|,)(\d{2})/, '$1'); result: '3876542' :( I'm trying to get '12323.09'
Thanks a lot for your help. match is really what I need
0

If the intent is to round to two places, you can use the following regex:

'12323.09876542'.replace(/(\.[0-9][0-9])\d+/, '$1');

Or, you can use the toFixed() Number function:

Number('12323.09876542').toFixed(2)

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.