简体   繁体   中英

Why is the following regex removing the bounding characters inside single quotes?

This code turns straight single quotes into curly single quotes:

var result = 'This \'is an\' example'.replace(/(?<!\w)\'\S(.*?)\S\'(?!\w)/g, '&lsquo;$1&rsquo;')
alert(result)

I thought the output would be:

This 'is an' example

But the output was this:

This 's a' example

I'm not sure why the bounding characters inside the quotes are being removed.

Why is this and how to fix it?

https://jsfiddle.net/gz5wjoqx/

You are matching the two \\S parts without capturing them:

.replace(/(?<!\w)\'\S(.*?)\S\'(?!\w)/g
//                 ^^     ^^

So when you replace with the first capture group surrounded by quotes:

'&lsquo;$1&rsquo;'
//      ^^

The characters in the \\S are not in the (.*?) capture group, so they're not included in the $1 replacement.

Put everything you want to replace with into the capture group:

 var result = 'This \\'is an\\' example' .replace( /(?<!\\w)'(\\S.*?\\S)'(?!\\w)/g, '&lsquo;$1&rsquo;' ); console.log(result)

(also note that ' doesn't need to be escaped in a pattern)

You can also consider using \\B ("not a word boundary") instead of negative lookaround for \\w , which will make the pattern compatible with older browsers and more concise:

 var result = 'This \\'is an\\' example' .replace( /\\B'(\\S.*?\\S)'\\B/g, '&lsquo;$1&rsquo;' ); console.log(result)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM