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?

4 Answers 4

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'));

Sign up to request clarification or add additional context in comments.

2 Comments

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.
yeah,@jmcgriz,i know that,that depends on the specific situation,this code behaves more universal
1

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

you can try it here

Comments

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);

Comments

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.

2 Comments

it will fail if some comas are present into Remarks field ;)
no, i take only first and last elements. i dont care how much commas included in remarks -))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.