How can I replace multiple characters with one character in javascript?

I have this line:

var str = '#this #is____#a test###__'

      

I want to replace all character (#, _) with (#), so the output file is:

'#this #is#a test#'

      

Note: I didn't know how much sequence (#) or (_) is in a line

what i am trying:

I am trying to write:

var str = '#this #is__ __#a test###__'
str = str.replace(/[#_]/g,'#')
alert(str)

      

But the way out was:

#this #is## ###a test#####

      

my attempt online

I am trying to use sequence (*) for sequence. But doesn't work:

var str = '#this #is__ __#a test###__'
str = str.replace(/[#_]*/g,'#')
alert(str)

      

So how can I get my output file?

+3


source to share


2 answers


A well-written RegEx can handle your problem quite easily. Quoting Mohit's answer to have a starting point:

var str = '#this #is__ __#a test###__';
var formattedStr = str.replace(/[#_,]+/g, '#');
console.log( formattedStr );

      

Line 2:
Put in formattedStr the result of the replace method on str.
How to replace a job? The first parameter is string or RegEx.
Note. RegExps in Javascripts are RegExp objects, not strings. So, I write

/yourRegex/

      

or

New RegExp('/yourRegex/')

      



is the equivalent syntax. Now let's discuss this particular RegEx.
Leading and trailing slashes are used to surround the pattern, and the g at the end means "globally" - not to stop after the first match.
Square parentheses describe the set of characters that can be supplied according to the pattern, while the + sign means "1 or more of this group".
Basically, ### will match, but also # or #### # _ # because _ and # belong to the same set.
The preferred behavior will be given with (# | _) +
This means "# or _", then, once found, keep looking for more or the selected pattern. "
So ___ will match as well as #### will, but __ ## will represent two different groups of matches (the first is __, the second is ##).
Another problem is that you don't know why to replace a pattern found with _ or #.
Fortunately, using parentheses allows us to use something called capture groups. Basically you can store whatever pattern you find in temporary variables that can be used in the replacement pattern.
Calling them is easy, looping $ to the position of the matched (pattern).
/ (foo) textnotgetting (bar) capture (baz) / for example, would populate the "variables" of the capture groups like this:

$1 = foo
$2 = bar
$3 = baz

      

In our case, we want to replace 1+ characters with only the first occurrence, and the + sign is not included in the parentheses!
So we can just

str.replace("/(#|_)+/g", "$1");

      

For this to work.

Have a nice day!

+2


source


Your regex replaces one instance of any matching character with the ie character you specify #

. You need to add a modifier +

to tell that any number of consecutive matching characters (_, #) should be replaced instead of each character individually. Modifier +

means that one or more occurrences of the specified pattern are matched at one time. You can read more about modifiers on this page:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp



var str = '#this #is__ __#a test###__';
var formattedStr = str.replace(/[#_,]+/g, '#');
console.log( formattedStr );
      

Run codeHide result


+1


source







All Articles