Home > other >  Adding character on the right side of match when match appears in isolation
Adding character on the right side of match when match appears in isolation

Time:12-01

I want to add ɨ on the right side of , , ʂ, ʐ, ts, ts', and s when they appear in isolation (when they are not touching other IPA characters). Note that there's a number after each of them, and the lines are surrounded by /.

const input = `/ʊɔ3 yɛn2 tʂ2 tʂɑʊ3/
/ʊɔ3 mən5 tɕ'ʂ2 tʂɑʊ3/
/pu2 ʂ4 tʂə4 kə4/
/ʂ1/`

const output = input.replace(/(\/|[ ])(tʂ|tʂ\'|ʂ|ʐ|ts|ts\'|s)\d([ ]|\/)/g, '$&ɨ')
console.log(output)

Right now, this is the output:

/ʊɔ3 yɛn2 tʂ2 ɨtʂɑʊ3/
/ʊɔ3 mən5 tɕ'ʂ2 tʂɑʊ3/
/pu2 ʂ4 ɨtʂə4 kə4/
/ʂ1/ɨ

But what I want is this:

/ʊɔ3 yɛn2 tʂɨ2 tʂɑʊ3/
/ʊɔ3 mən5 tɕ'ʂ2 tʂɑʊ3/
/pu2 ʂɨ4 tʂə4 kə4/
/ʂɨ1/

CodePudding user response:

const input = `/ʊɔ3 yɛn2 tʂ2 tʂɑʊ3/
/ʊɔ3 mən5 tɕ'ʂ2 tʂɑʊ3/
/pu2 ʂ4 tʂə4 kə4/
/ʂ1/`

const output = input.replace(/(\/|[ ])(tʂ|tʂ\'|ʂ|ʐ|ts|ts\'|s)(\d([ ]|\/))/g, '$1$2ɨ$3')
console.log(output)

(included the digit in the third capture and changes the replacement text)

CodePudding user response:

What about using:

([/ ](?:t[ʂs]'?|[ʂʐs])(?=\d [ /]))

And replace by $1ɨ. See an online demo


  • ([/ ] - Open 1st capture group and match a capture forward slahs or space;
    • (?:t[ʂs]'?|[ʂʐs]) - Non-capture group to match any of the given combinations you provided;
    • (?=\d [ /]) - Positive lookahead to assert position is followed by 1 digits and another forward slash or space;
    • ) - Close 1st capture group which is now ready for backreference in replacement.

const input = `/ʊɔ3 yɛn2 tʂ2 tʂɑʊ3/
/ʊɔ3 mən5 tɕ'ʂ2 tʂɑʊ3/
/pu2 ʂ4 tʂə4 kə4/
/ʂ1/`

const output = input.replace(/([/ ](?:t[ʂs]'?|[ʂʐs])(?=\d [ /]))/g, '$1ɨ')
console.log(output)

  • Related