RegEx to replace variable quantity of characters with single character - javascript

Given this string
var d = 'The;Quick;;Brown;Fox;;;;;;Jumps';
What RegEx would I need to convert to this string:
'The,Quick,Brown,Fox,Jumps'
I need to replace 1-n characters (e.g. ';') with a single character (e.g. ',').
And because I know that sometimes you like to know "what are you trying to accomplish??"
I need to condition a string list of values that can be separated with a combination of different methods:
'The , Quick \r\n Brown , \r\n Fox ,Jumps,'
My approach was to convert all known delimiters to a standard character (e..g ';') and then replace that with the final desired ', ' delimiter

as Josh Crozier says, you can use
d = d.replace(/;+/g, ',');
You can also to the whole thing in one operation with something like
d = d.replace(/[,; \r\n]+/g, ',');
The [,; \r\n]+ part will find groups that are made of commas, semicolons, spaces etc.. Then the replace will replace them all with a single comma. You can add any other characters you want to treat as delimiters in with the brackets.
EDIT: actually, it's probably better to use something like this. The \s will match any whitespace character.
d.replace(/[,;\s]+/g, ',');

This should do the trick:
d.replace(/[;]+/g, ',')
It just replaces all group of semicolons together for a comma

Related

Replace comma as separator

I am trying to build a Regex to replace commas used as a separator in between normal text.
Different ways I can replace that is valid:
Space before comma
Comma is between text and/or numbers, without any space
Several commas after each other
Example:
"This is a text separated with comma, that I try to fix. , It can be split in several ways.,1234321 , I try to make all the examples in one string,,4321,"
Results:
This is a text separated with comma, that I try to fix.
It can be split in several ways.
1234321
I try to make all the examples in one string
4321
This is the code I have so far using Node.js / Javascript:
data.replace(/(\S,\S)|( ,)|(,,)|(,([a-z0-9]))/ig,';')
The answer from #torazaburo work best, except for several commas with space in-between (, , , ,)
console.log(str.split(/ +, *|,(?=\w|,|$)/));
var str = "This is a text separated with comma, that I try to fix. , It can be split in several ways.,1234321 , I try to make all the examples in one string,,4321,";
console.log(str.split(/ +, *|,(?=\w|,|$)/));
This will split on any comma preceded by one or more spaces, no matter what follows (and eat the preceding spaces, and following spaces if any); or, any comma followed by an alphanumeric or comma or end-of-string.
There is no easy way with the regexp to get rid of the final empty string in the result, caused by the comma at the very end of the input. You can get rid of that yourself if you don't want it.
To rejoin with semi-colon, add .join(';').
data.replace(/\s*,+\s*/g, ';');
This will yield:
This is a text separated with comma;that I try to fix.;It can be split in several ways.;1234321;I try to make all the examples in one string;4321;
There are three parts to this:
\s*: Match zero or more whitespace characters.
,+: Match one or more commas.
\s*: Match zero or more whitespace characters.
If, instead, you want to replace any number of consecutive commas with a single semi-colon:
data.replace(/,+/g, ';');
Honestly, I'm not sure I understood your requirements. If I did misunderstand, please provide the output string you're expecting.

regex and replace string

Im trying to format my value accepting only digits and also replacing comma by dot, but i cant figure replace the comma by dot.
Example:
"4,82 €".replace(/[^\d\,]/g, '')
Expeted Value:
4.82
Try this: '4,82 €'.replace(/[^0-9,]/g, '').replace(/,/g, '.');
Also, you don't need to escape a ,. It has no reserved meaning in regex
Try handling this through two separate expressions, one to remove and the other to perform your replacement :
Remove all non digits and commas.
Replace all commas with periods.
This might look something like the expression below :
// Replaces all non-digits and commas /[^\d,]/ and replaces commas /,/
input.replace(/[^\d,]/g, '').replace(/,/g,'.`');
Example
// Replace any non-digit or commas and then replace commas with periods
console.log("4,82 € -> " + "4,82 €".replace(/[^\d\,]/g, '').replace(/,/g,'.'))
// If you want to allow periods initially, then don't remove them
console.log("4.82 € -> " + "4.82 €".replace(/[^\d\,.]/g, '').replace(/,/g,'.'))
You are almost right, just missed one small thing.
Your regex
[^\d\,]
will replace only one non-digit or comma character and not all.
Change your regex to
[^\d\,]+
like this
"4,82 €".replace(/[^\d\,]+/g, '')
and it will work

Split string on spaces except for in quotes, but include incomplete quotes

I am trying to split a string in JS on spaces except when the space is in a quote. However, an incomplete quote should be maintained. I'm not skilled in regex wizardry, and have been using the below regex:
var list = text.match(/[^\s"]+|"([^"]*)"/g)
However, if I provide input like sdfj "sdfjjk this will become ["sdfj","sdfjjk"] rather than ["sdfj",""sdfjjk"].
You can use
var re = /"([^"]*)"|\S+/g;
By using \S (=[^\s]) we just drop the " from the negated character class.
By placing the "([^"]*)" pattern before \S+, we make sure substrings in quotes are not torn if they come before. This should work if the string contains well-paired quoted substrings and the last is unpaired.
Demo:
var re = /"([^"]*)"|\S+/g;
var str = 'sdfj "sdfjjk';
document.body.innerHTML = JSON.stringify(str.match(re));
Note that to get the captured texts in-between quotes, you will need to use RegExp#exec in a loop (as String#match "drops" submatches).
UPDATE
No idea what downvoter thought when downvoting, but let me guess. The quotes are usually used around word characters. If there is a "wild" quote, it is still a quote right before/after a word.
So, we can utilize word boundaries like this:
"\b[^"]*\b"|\S+
See regex demo.
Here, "\b[^"]*\b" matches a " that is followed by a word character, then matches zero or more characters other than " and then is followed with a " that is preceded with a word character.
Moving further in this direction, we can make it as far as:
\B"\b[^"\n]*\b"\B|\S+
With \B" we require that " should be preceded with a non-word character, and "\B should be followed with a non-word character.
See another regex demo
A lot depends on what specific issue you have with your specific input!
Try the following:
text.match(/".*?"|[^\s]+/g).map(s => s.replace(/^"(.*)"$/, "$1"))
This repeatedly finds either properly quoted substrings (first), OR other sequences of non-whitespace. The map part is to remove the quotes around the quoted substrings.
> text = 'abc "def ghi" lmn "opq'
< ["abc", "def ghi", "lmn", ""opq"]

Regex to format price

I have some price tags like $23.47,-$50.00, $0.37, $113.57,$600,0456.00 and so on. I intend to remove dollar($),thousand separator(,) and also any white space.
For this purpose I am using regex as
(Sometext).replace(/^[, ]+|[, ]+$|[, ]+/g, "").replace('$',"").replace(/\s+/g, ' ');
Is there any better way to combine all this operations(remove thousand separator comma, remove dollar symbol, remove white space) with single replace?
Can you just use the regex OR operator? Something like this:
(Sometext).replace(/^[, ]+|[, ]+$|[, ]+|\$|\s+/g, "");
The | represent an OR in regex. I just placed the pipe between the three patterns you want to match, and used them within a single replace(). I also escaped the $ in dollar sign matching statement.
Based on this this answer try:
var price = "$23 . 47"
var simpleValue = price.match(/(\d+)/g).join("")
You can use something like
'$444,55,22.33'.replace(/[,\s$]/g, "");
You could target anything NOT what you want to keep.
So, a regex like: price.replace(/[^\d\.-]/g, '');
The ^ in the brackets tells it to match anything but the included characters.
The \d allows any numeric character.

javascript replace all occurrences ",\S" with ", \S"

I want to have spaces separating items in a CSV string. That is "123,456,789" => "123, 456, 789". I have tried, but been unable to construct a regexp to do this. I read some postings and thought this would to the trick, but no dice.
text = text.replace(new RegExp(",\S", "g"), ", ");
Could anyone show me what I am doing wrong?
You have two problems:
Backslashes are a pain in the, um, backslash; because they have so many meanings (e.g. to let you put a quote-mark inside a string), you often end up needing to escape the backslash with another backslash, so you need ",\\S" instead of just ",\S".
The \S matches a character other than whitespace, so that character gets removed and replaced along with the comma. The easiest way to deal with that is to "capture" it (by putting it in parentheses), and put it back in again in the replacement (with $1).
So what you end up with is this:
text = text.replace(new RegExp(',(\\S)', "g"), ", $1");
However, there is a slightly neater way of writing this, because JavaScript lets you write a regex without having a string, by putting it between slashes. Conveniently, this doesn't need the backslash to be escaped, so this much shorter version works just as well:
text = text.replace(/,(\S)/g, ", $1");
As an alternative to capturing, you can use a "zero-width lookahead", which in this situation basically means "this bit has to be in the string, but don't count it as part of the match I'm replacing". To do that, you use (?=something); in this case, it's the \S that you want to "look ahead to", so it would be (?=\S), giving us this version:
text = text.replace(/,(?=\S)/g, ", ");
There are 2 mistakes in your code:
\S in a string literal translates to just S, because \S is not a valid escape sequence. As such, your regex becomes /,S/g, which doesn't match anything in your example. You can escape the backslash (",\\S") or use a regex literal (/,\S/g).
After this correction, you will replace the character following the comma with a space. For instance, 123,456,789 becomes 123, 56, 89. There are two ways to fix this:
Capture the non-space character and use it in the replacement expression:
text = text.replace(/,(\S)/g, ', $1')
Use a negative lookahead assertion (note: this also matches a comma at the end of the string):
text = text.replace(/,(?!\s)/g, ', ')
text = text.replace(/,(\S)/g, ', $1');
try this:
var x = "123,456,789";
x = x.replace(new RegExp (",", "gi"), ", ");

Categories

Resources