I'm using javaScript RegEx literals to replace parts of a string, i want to use | to check for different types of patterns that may occur and () to capture a part of the match i want to replace. it doesn't seem to work though, why is this?
here's my current regEx:
testText.replace(/(\w)\s+\w$|(\w)\s+$|\w+(\w)$/g, "$1...")
I'm guessing somethings wrong with it: here's want i want to acheieve:
given "xx" i expect:
"x..."
given "x " I expect:
"x...
given "werfdsdfasd" (word with no space) I expect:
"werfdsdfasd..."
The problem is that you have 3 capture groups and you always reference the first one.
Try this: (\\w+)(?:\\s+\\w$|\\s+$|$)
因此,您想捕获空格或行尾之前的所有内容。
(\w+)(\s+.*$|$)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.