0

I have a some lengthy text in a JS variable that I intend to pass to a html textbox but before that I want to trim it down to just the relevant text. If for instance I have this:

Subject_name: English, Teacher_name: John Doe, Remarks: some comment, Overall_marks:87,
Subject_name: Mathematics, Teacher_name: Doe John, Remarks: some comment, Overall_marks:75,
Subject_name: Science, Teacher_name: JD, Remarks: some comment, Overall_marks:80,

I want to replace "Teacher_name: John Doe, Remarks: some comment, " with an empty string "" so as to achieve:

Subject_name: English, Overall_marks:87,
Subject_name: Mathematics, Overall_marks:75,
Subject_name: Science, Overall_marks:80,

So basically I want to select "Teacher_name" as the start, to "comment, " as the end but I'm not really sure how to achieve that. The examples I'm seeing around and questions here on SO are for single characters which won't work in this case.

Such an example was - .replace(/H.*S/, ''); to replace everything between H and S with an empty string from this question - I tried to use my start and end string selectors above which replaced everything from the very first start selector to the last occurence of the last selector, resulting in Subject_name: English, Overall_marks:80, which is not what I need. Tried to put it in an array but resulted in random characters (not conversant with regex hence the chaos).

How can I achieve the replace I'm trying to implement?

Clint_A
  • 518
  • 2
  • 11
  • 35

4 Answers4

2

try this:str.replace(/Teacher_name.*?Overall_marks/g, 'Overall_marks')

let str=`Subject_name: English, Teacher_name: John Doe, Remarks: some comment, Overall_marks:87,
Subject_name: Mathematics, Teacher_name: Doe John, Remarks: some comment, Overall_marks:75,
Subject_name: Science, Teacher_name: JD, Remarks: some comment, Overall_marks:80,`;
console.log(str.replace(/Teacher_name.*?Overall_marks/g, 'Overall_marks'));
xianshenglu
  • 4,943
  • 3
  • 17
  • 34
  • If it's for a very large dataset, `.*?` might wind up being a bit slow, so you could also use `str.replace(/Teacher_name[^,]+,[^,]+,\s?/g, '')`. The more specific you can be, the quicker your regex will run. – jmcgriz Apr 10 '18 at 15:35
  • yeah,@jmcgriz,i know that,that depends on the specific situation,this code behaves more universal – xianshenglu Apr 11 '18 at 02:31
1

You can use /Teacher_name:[\w\s]+,\s*Remarks:[\w\s]+,\s*/gi

you can try it here

Flying_whale
  • 377
  • 1
  • 16
1

Try it

var input = `Subject_name: English, Teacher_name: John Doe, Remarks: some comment, Overall_marks:87,
Subject_name: Mathematics, Teacher_name: Doe John, Remarks: some comment, Overall_marks:75,
Subject_name: Science, Teacher_name: JD, Remarks: some comment, Overall_marks:80,`;

var output = input.replace(/Teacher_name:.*?(?=Overall_marks)/g,'');

console.log(output);
Rehan Haider
  • 893
  • 11
  • 26
0

You may not need regex if lines are described as above.

var splitted = sentence.split(",");
splitted[0] + splitted[splitted.length - 1]

will work on most cases.

marmeladze
  • 6,468
  • 3
  • 24
  • 45