admin管理员组

文章数量:1313615

In the string

somethingcryptic12A@#$~` abc def@#

, I would like to replace the first word by new so that it would then be new

new abc def@#

. How can I do this using regular expression. I have made the first word cryptic to indicate that it can contain any character and any number of characters. It's the first word if there is a space after it.

In the string

somethingcryptic12A@#$~` abc def@#

, I would like to replace the first word by new so that it would then be new

new abc def@#

. How can I do this using regular expression. I have made the first word cryptic to indicate that it can contain any character and any number of characters. It's the first word if there is a space after it.

Share Improve this question asked Feb 1, 2013 at 19:22 dreamerkumardreamerkumar 1,5501 gold badge18 silver badges28 bronze badges 1
  • In my defense, I was able to e up with the full solution after some initial help from Some1. I had tried some solutions like [a-zA-Z] but that wouldn't work for all scenarios. – dreamerkumar Commented Feb 1, 2013 at 19:29
Add a ment  | 

1 Answer 1

Reset to default 9

You can use this regular expression

^\S+

^ represents start of the string

\S matches any character except space..

+ matches preceding character 1 to many times

本文标签: regexreplace the first word of a javascript string using regular expressionStack Overflow