-2

what is way to find if string has new line character(/n) followed by any other character in javascript? Currently I'm doing this:

if (((mystring).slice(-2)) == "/\n/A") {
    //mycode
}

I want to check whether last two characters of string are \n and A. My thing is not working. Is there any other way to do this?

0

1 Answer 1

3

I want to check whether last two characters of string are \n and A.

You can use String#endsWith

const A = `this is a valid string\nA`;
const B = `this is an invalid string`;

console.log('A', A.endsWith('\nA'));
console.log('B', B.endsWith('\nA'));

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.