2

Could anyone help out with regex expression I can run in node to change that changes only the letter that appears immediately after the dot(.) to lower-case? I am reading And writing back the changed text back to the same text file.

Input:

$.Carpool[0].NoofSeats], [$.Carpool[1].NoofSeats]

So after replace with regex the updated text file should be:

Desired Output:

[$.carpool[0].noOfSeats], [$.carpool[1].noOfSeats]
Community
  • 1
  • 1
Eva
  • 109
  • 9

2 Answers2

2

A dot followed by an uppercase letter can be matched with \.[A-Z].

To convert it to lowercase, you can do

str = str.replace(/\.[A-Z]/g, (m0) => m0.toLowerCase());
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • Great solution! Just a little improvement you can replace (m0) => m0.toLowerCase() for m0 => m0.toLowerCase() to make it even easier to read. – Pablo Jun 15 '19 at 18:34
  • ok @melpomene at the end that is just a question of styles and styles are subjectives. – Pablo Jun 15 '19 at 18:41
  • @melpomene Reposting the question- I have two folders, input and output folder with many text files in the above format. How do I read all the files from the input folder run the regex and write all the updated files to another output folder?I am using nodejs. I got to reading all files from the dir. – Eva Jun 16 '19 at 02:55
-1

These expressions would simply work here:

\.([A-Za-z])
\.([A-Z])

being replaced by \.\L$1, which \L or \U may not be supported/available in JavaScript, instead of which, we would simply apply .toLowerCase(); function.

Demo

Community
  • 1
  • 1
Emma
  • 27,428
  • 11
  • 44
  • 69
  • melpomene and Emma both of your solutions worked. Thank you. – Eva Jun 15 '19 at 18:41
  • 1
    Now I have two folders, input and output folder with many text files in the above format. How do I read all the files from the input folder run the regex and write all the updated files to the output folder. I am using nodejs. – Eva Jun 15 '19 at 18:42
  • 1
    Thanks for sharing the post Emma. That post has it for reading only one file from a directory. I would want to read all the files in the folder and give the output for all the files in the output directory. – Eva Jun 15 '19 at 18:52