3

str.replace(/\w*(\ || --> A marker whitch can represent any char or string? <-- || )/g, function() {return "(" + arguments[0] + ")"})

Freezy Ize
  • 1,201
  • 2
  • 10
  • 11
  • This question is related to OP's [previous question](http://stackoverflow.com/q/15042565/1169519). – Teemu Feb 24 '13 at 11:09

2 Answers2

5

The dot (.) matches any (1) character; .+ matches a string of at least length 1, .* matches a string of at least length 0.

robertklep
  • 198,204
  • 35
  • 394
  • 381
3

A period is used to represent any character that is not a line break, but to represent any character you can use a set with two complementing sets, like all alphanumeric characters and all non-alphanumeric characters:

str = str.replace(/\w*([\W\w])/g, "($1)");

That will match a single character, if you want to match more than one you have to specify how many. [\W\w]{1,3} would for example match one to three characters. [\W\w]+ would match everything to the end of the string.

Note that you don't need a callback for a simple replacement like this, just a string where $1 is substituted with the first caught value.


Edit:
Come to think of it, as the character is following a set that matches alphanumeric characters, it has to be non-alphanumeric, so just \W will do:

    str = str.replace(/\w*(\W)/g, "($1)");
Guffa
  • 687,336
  • 108
  • 737
  • 1,005