str.replace(/\w*(\ || --> A marker whitch can represent any char or string? <-- || )/g, function() {return "(" + arguments[0] + ")"})
-
This question is related to OP's [previous question](http://stackoverflow.com/q/15042565/1169519). – Teemu Feb 24 '13 at 11:09
2 Answers
The dot (.
) matches any (1) character; .+
matches a string of at least length 1, .*
matches a string of at least length 0.

- 198,204
- 35
- 394
- 381
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)");

- 687,336
- 108
- 737
- 1,005
-
@FreezyIze: When I try it, I get exactly the expected output. Demo: http://jsfiddle.net/EaJSq/1/ – Guffa Feb 23 '13 at 20:21
-
-
-
@FreezyIze: That does the same, except it doesn't work for *all* characters. I already covered that in the first sentence of my answer. – Guffa Feb 23 '13 at 20:55