1

I found an answer in this question that is almost perfect but I need a slight tweak. I've read the basics of regex and I'm using https://regex101.com/ but I can't figure it out.

Using "string".replace(/\d+$/, '') I can remove any numbers from the end of the string. However I also need to remove any numbers that have a format with a decimal like #.# or ##.# as well as whole numbers, but only when they appear at the end of the string. From regex101 I have found that the $ is for the end of the string.

I can use .replace(/\d+([.]\d+)?/g) to remove numbers and floats but it removes them from the entire string, not just when they appear at the end, and I can't work out where to put the $ as I don't really understand regex yet and can't get it to work.

It seems such a small and stupid problem but I'd appreciate the help.

Thanks

Lyall
  • 1,367
  • 3
  • 17
  • 42
  • 1
    You seem to need `.replace(/\d+(?:\.\d+)?$/, '')` – Wiktor Stribiżew Jul 13 '17 at 18:32
  • @WiktorStribiżew thank you, perfect! I don't understand it but it does work, thank you. I will read up more about regex. – Lyall Jul 13 '17 at 18:33
  • 1
    There is another /(\d+\.\d+)+$|\d+$/ – Henrique Oecksler Bertoldi Jul 13 '17 at 18:35
  • Why did you accept an answer that doesn't parse int's/decimals correctly ? `(?:\d+(?:\.\d*)?|\.\d+)$` .. -1 –  Jul 13 '17 at 20:06
  • @sln because I tested it and it worked for me on a live example. – Lyall Jul 13 '17 at 20:15
  • @Lyall - Well, I just said it doesn't work. Your sample _you_ used is what _you_ think a int/float is, not what it really constitutes. –  Jul 13 '17 at 20:18
  • @sln Well I had a site that was displaying a int and a decimal (depending on the querystring) at the end, I added the suggested answer and it is no longer showing. Therefore as far as I'm concerned it works in the live environment and I accepted the answer. Can you prove it doesn't work? I don't feel the downvote is necessary considering this fixed my problem exactly. – Lyall Jul 13 '17 at 20:21
  • @sln also I don't see what right you have to downvote a question because you think I have accepted the wrong answer when you yourself have not submitted an alternative answer that works any better. The answer I accepted works for me, so I accepted it. Thanks. – Lyall Jul 13 '17 at 20:24
  • Your words: `to remove numbers and floats`. Go ahead and change that to `exclude valid floats that I don't think are valid` and I'll take off the downvote. –  Jul 13 '17 at 20:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/149168/discussion-between-lyall-and-sln). – Lyall Jul 13 '17 at 20:26
  • @Lyall There are different locales, environments and just definitions of a "float" (or people also call it "decimal" interchangeably). For most human cases, `\d+(?:\.\d+)?` suffices. In computing, there are other "notations": `.5`, `1.2e+45`, `6.` are possible. Then, even [this pattern](http://www.regular-expressions.info/floatingpoint.html) won't be enough. You can use [`\s*[-+]?(?:[0-9]+\.?[0-9]*|\.[0-9]+)(?:[eE][-+]?[0-9]+)?$`](https://regex101.com/r/MFyXeC/2). But *universal* patterns can overfire. Use only the part that you need and do not listen to people who *tell* you what you need. – Wiktor Stribiżew Jul 14 '17 at 20:21
  • And if you need more advice on the regex implementation in your concrete context, feel free to drop me a line. – Wiktor Stribiżew Jul 14 '17 at 20:22

1 Answers1

3

You may use

.replace(/\d+(?:\.\d+)?$/, '')

The pattern matches

  • \d+ - one or more digits
  • (?:\.\d+)? - a non-capturing group (the ?: makes it non-capturing, that is, you cannot access the value captured by this group) that matches an optional sequence of a . and then 1+ digits
  • $ - end of string.

To also remove any 0+ whitespaces before the number, add \s* (where \s matches any whitespace and * quantifier makes the regex engine match (consecutively) 0 or more of the characters matched with this pattern) at the pattern start.

See the regex demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    Some more reference: [`$` anchor](https://stackoverflow.com/documentation/regex/1603/anchor-characters-dollar), [non-capturing groups](https://stackoverflow.com/a/3513858/3832970), [regex quantifiers](http://www.rexegg.com/regex-quantifiers.html#basics). – Wiktor Stribiżew Jul 13 '17 at 18:46