Replace all occurrences of character except in the beginning of string (Regex) - javascript

I'm trying to get rid of all minuses/dashes in a string number, except the first occurrence. After fiddling with Regex (JavaScript) for half an hour, still no results. Does anyone know the fix?
Given:
-123-45-6
Expected:
-123456
Given:
789-1-0
Expected:
78910

This one will do as well(it means dashes not at the beginning of the string):
(?!^)-
Example:
text = "-123-45-6".replace(/(?!^)-/g, "");

A simple solution :
s = s.replace(/(.)-/g,'$1')

Jutr try with:
'-123-45-6'.replace(/(\d)-/g, '$1');

Related

replace first occurrence of string after another string

Tried to find it in the network without any success..
Let's say I have the following string:
this is a string test with a lot of string words here another string string there string here string.
I need to replace the first 'string' to 'anotherString' after the first 'here', so the output will be:
this is a string test with a lot of string words here another anotherString string there string here string.
Thank you all for the help!
You don't need to add g modifier while replacing only the first occurance.
str.replace(/\b(here\b.*?)\bstring\b/, "$1anotherString");
DEMO
If you are looking for something which takes in a sentence and replaces the first occurrence of "string" after "here" (using the example in your case),
You should probably look at split() and see how to use it in a greedy way referring to something like this question. Now, use the second half of the split string
Then use replace() to find "string" and change it to "anotherString". By default this function is greedy so only your first occurrence will be replaced.
Concatenate the part before "here" in the original string, "here" and the new string for the second half of the original string and that will give you what you are looking for.
Working fiddle here.
inpStr = "this is a string test with a lot of string words here another string string there string here string."
firstHalf = inpStr.split(/here(.+)?/)[0]
secondHalf = inpStr.split(/here(.+)?/)[1]
secondHalf = secondHalf.replace("string","anotherString")
resStr = firstHalf+"here"+secondHalf
console.log(resStr)
Hope this helps.

I want to find numbers-include dot and comma- but Regex not working in javascript

I thought it was very simple to find out. But how many ways I tried still not work properly.
Below is the test snippet.
"100$ and 1.000,000EUR 1,00.0.000USD .90000000000000000000$ (09898)".replace(/[\.,\d]*/g, '{n}')
And I want the result like below.
{n}$ and {n}EUR {n}USD {n}$ ({n})
The * is your problem, change the regex to /[.,\d]+/g instead.
"100$ and 1.000,000EUR 1,00.0.000USD .90000000000000000000$ (09898)".replace(/[.,\d]+/g, '{n}');
Output
{n}$ and {n}EUR {n}USD {n}$ ({n})
JSFiddle Example Check console screen for the output.
The problem here is that [\.,\d]* can match an empty string. The first step would be to use [.,\d]+ so that at least one of these characters matches.
But a better regex would be \d[.,\d]* because it ensures the replaced characters begin with a digit, so it won't replace periods in sentences.
If you want to go further, you can also use (?=[.,\d]*\d)[.,\d]+ if to handle numbers starting with periods. This one would be the proper answer for your case. The lookahead ensures there's at least one digit anywhere in the replaced text.
Note that you don't need to escape the . inside a character class.
\.?\d[^\s]*\d
Try this.Replace with {n}.See demo.
http://regex101.com/r/kP8uF5/3
var re = /\.?\d[^\s]*\d/gm;
var str = '100$ and 1.000,000EUR 1,00.0.000USD .90000000000000000000$ (09898)';
var subst = '{n}';
var result = str.replace(re, subst);

Insert a character after every n characters but only for the first x occurrences

I have a string of 13 characters say XXXXXXXXXXXXX. I wish to enter a hyphen after every three characters but only for the first three occurrences using JQuery and possibly Regular Expressions.
Which means I need my string to be XXX-XXX-XXX-XXXX.
If I use str.replace(/(.{3})/g, "$1-"), str being my string, as I came across in another post, it yields me XXX-XXX-XXX-XXX-X.
Any help would be greatly appreciated.
Thanks guys.
What about .replace(/(.{3})(.{3})(.{3})/,'$1-$2-$3-')?
If you want to do it with Regex this could work...
// Not nice but works
var text = "XXXXXXXXXXXXX";
text.replace(/(...)(...)(...)(....)/g, "$1-$2-$3-$4");
// Result "XXX-XXX-XXX-XXXX" Chrome 27+

How can I get the last part of a songkick URL using regex?

I need to get the last part after the slash from this URL or even just the number using regex:
http://www.songkick.com/artists/2884896-netsky
Does anyone know how I can go about doing this?
Many thanks,
Joe
I've got a regex pattern to find everything after the last slash - it is ([^//]+)$ . Thanks!
don't know if this is ok for you:
last part:
"http://www.songkick.com/artists/2884896-netsky".replace(/.*\//,"")
"2884896-netsky"
number:
"http://www.songkick.com/artists/2884896-netsky".replace(/.*\//,"").match(/\d+/)
["2884896"]
Try this:
var s = "http://www.songkick.com/artists/2884896-netsky";
s.match(/[^\/]+$/)[0]
[^\/]+ matches one or more characters other than /
$ matches the end of the string
For just the number, try:
var value = s.match(/(\d+)[^\/\d]+$/)[1];
value = parseInt(value, 10); // convert it to an Integer
(\d+) matches any Number and groups it
[^\/\d]+ matches anything besides Numbers or /
More Info on Javascript Regular Expressions

Extracting numbers from a string using regular expressions

I am clueless about regular expressions, but I know that they're the right tool for what I'm trying to do here: I'm trying to extract a numerical value from a string like this one:
approval=not requested^assignment_group=12345678901234567890123456789012^category=Test^contact_type=phone^
Ideally, I'd extract the following from it: 12345678901234567890123456789012 None of the regexes I've tried have worked. How can I get the value I want from this string?
This will get all the numbers:
var myValue = /\d+/.exec(myString)
mystr.match(/assignment_group=([^\^]+)/)[1]; //=> "12345678901234567890123456789012"
This will find everything from the end of "assignment_group=" up to the next caret ^ symbol.
Try something like this:
/\^assignment_group=(\d*)\^/
This will get the number for assignment_group.
var str = 'approval=not requested^assignment_group=12345678901234567890123456789012^category=Test^contact_type=phone^',
regex = /\^assignment_group=(\d*)\^/,
matches = str.match(regex),
id = matches !== null ? matches[1] : '';
console.log(id);
If there is no chance of there being numbers anywhere but when you need them, you could just do:
\d+
the \d matches digits, and the + says "match any number of whatever this follows"

Categories