0

I have an input field where the user is only allowed to use letters, spaces and commas.

I've created this here so far:

splitStr = splitStr.split(' ').join(', ');
splitStr = splitStr.split(',').join(', ');
splitStr = splitStr.split(';').join(', ');
splitStr = splitStr.split('-').join(', ');
splitStr = splitStr.split('_').join(', ');
splitStr = splitStr.split('/').join(', ');
splitStr = splitStr.split('#').join(', ');

$("imgTags").value = splitStr;

// removes duplicate spaces
splitStr = splitStr.replace(/ +(?= )/g,'');

// removes duplicate commas
splitStr = splitStr.replace(/,+/g,',');

// missing: remove ', ' duplicates

So this code above makes it so that the users input is always converted to a comma space and on the bottom of the code I'm removing artifacts that can happen, like duplicate commas or duplicate spaces.

In the first like you can see that I'm also replacing any space with comma space.. this gives me an artifact of , , , , , , , , , , , , , , , this means I need also to replaces any comma space comma space with just a single comma space, so I've tried to do this but I never get the the desired result.

How can I replace regex for space comma duplicates?

This: , , , , , , needs to become this , e.g. comma space comma space comma space needs to be just comma space.

2
  • 1
    Perhaps use a character class [ ,;_\/#-]+ and repeat that 1+ times and then replace with a comma space regex101.com/r/tUU24v/1 Commented Dec 31, 2019 at 12:57
  • 1
    do it with regex expression. You need change input or check for error validation? Commented Dec 31, 2019 at 12:58

3 Answers 3

2

very easy, something like this

str.replace(/(, )+/g, ", ")

or even in the very beginning

str.replace(/[-_;, ]+/g, ", ")
Sign up to request clarification or add additional context in comments.

Comments

2

Split string by non-letters and join with commas:

str.split(/[^A-Za-z]/).join(',')

replace duplicate commas:

str.replace(/,+/g,',');

replace comma with comma space

str.replace(/,/g,', ');

Comments

1

You could do all those replacements in one go using a repeated character class and replace the matches with a comma and a space.

Because the character class is repeated, it will match consecutive matches and use only a single replacement.

[ ,;_\/#-]+

Regex demo

const regex = /[ ,;_\/#-]+/g;
const str = `test,,,,,test; test-test/test`;
const subst = `, `;
const result = str.replace(regex, subst);

console.log(result);


If you don't want to replace when the characters are at the start or end of the line, you might use a callback function for the replacement:

let pattern = /(?:^[ ,;_\/#-]+|[ ,;_\/#-]+$)|([ ,;_\/#-]+)/g;
let str = "/#,test,,,test; test-test/test,#/";
str = str.replace(pattern, function(m, g1) {
  if (g1 !== undefined) {
    return ', ';
  }
  return m;
});
console.log(str);

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.