简体   繁体   中英

Javascript - how to replace an escape character followed by a 'G' in a string

I have the following string 'Ganaway\Gannaway\Gansway' from a text file where I need to replace the escape character(s) with ' or '.

I've found answers here on Stackoverflow that suggest:

const name = 'Ganaway\Gannaway\Gansway';
name.replace(/\//g, ' or ');

Fiddle

However when I debug it the name is already stripped automatically of the escape character before it reaches the.replace() code.

The result should be 'Ganaway or Gannaway or Gansway' but its unfortunately 'GanawayGannawayGansway'.

Update:

The strings originate from a GEDCOM file which was exported from a genealogy website called Ancestry.com - here are some examples of the original text, it's not only the 'G' but various others that lead to

Regular expression is invalid: PCRE does not support \L, \l, \N{name}, \U, or \u

errors:

1 NAME Emma Mae\May /Ganaway\Gannaway\Gansway/

or

1 NAME Niecy\Nicy Ann /Holy/

or

1 NAME Peggy Jo /Stewart\Uttrell/

You will have to use this syntax

const name = String.raw`Ganaway\Gannaway\Gansway`;
name.replace(/\\/g, ' or ');

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