Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  164] [ 2]  / answers: 1 / hits: 19144  / 11 Years ago, fri, march 15, 2013, 12:00:00

If I have the string hello and I want to replace the second and third character with _, how can i do that, given only the location of the substring, not what it actually is.


More From » html

 Answers
26
str = str.replace( /^(.)../, '$1__' );


The . matches any character except a newline.



The ^ represents the start of the string.



The () captures the character matched by the first . so it can be referenced in the replacement string by $1.



Anything that matches the regular expression is replaced by the replacement string '$1__', so the first three characters at the start of the string are matched and replaced with whatever was matched by the first . plus __.


[#79566] Thursday, March 14, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
trevionbronsonr

Total Points: 160
Total Questions: 85
Total Answers: 110

Location: Bonaire
Member since Wed, Mar 29, 2023
1 Year ago
trevionbronsonr questions
;