Skip to content
Advertisement

Check if match is from right or left side of the stringB and return the difference

We have this code:

JavaScript

Here is the rule:

stringA always is a part of stringB and it matches some portion of stringB from right or left.

For instance in the above code we have stringA which is matching a part of stringB from the right of stringB . right?

The second rule is we want to fill notIncluded with a portion of stringB which is not included in stringA… As our code suggests… ok?

Now I need a function to give us the same notIncluded just as the above code (stringB – stringA) somehow!!!

But I want another functionality here:

I want an if else statement to check if the matching is from right side of the stringB (like what we have in the above) or from left side.

So if we have these:

JavaScript

the notIncluded would be :

JavaScript

and the check (if statement or the variable) shows the match is from right.

In opposite if we have these:

JavaScript

the notIncluded would be the same again:

JavaScript

This time the check (if statement or the variable) shows the match is from left side of the stringB.

Advertisement

Answer

You could just use string.startsWith and string.endsWith.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement