0

Trying to check if randomString starting with just. (including the dot).

This should give me false but it's not the case:

var randomString = 'justanother.string';
	
var a = randomString.match('^just\.');
	
console.log(a);

I probably missed something in the regex argument.

4 Answers 4

4

You need to use create a Regular Expression and the use .test() method.

var randomString = 'justanother.string';
var a = /^just\./.test(randomString)
console.log(a);

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

Comments

1

The answer is simple, you didn't create regex propertly.
'this is not regex'
/this is regex/ new RexExp('this is also regex')

var randomString = 'justanother.string';
	
var a = randomString.match(/^just\./);

console.log(a);

// I sugest dooing something like this
const startsWithJust = (string) => /^just\./.test(string)

Comments

1

var randomString = 'justanother.string';
var another = 'just.....................';
console.log(  randomString.match('^(just[.]).*') );
console.log(  another.match('^just[.].*') );

1 Comment

noo this would match just. but also just and just.......................................................
0

If you wish to keep your lines the same only one change is needed.

var a = randomString.match('^just\\.');

you need to escape the first backslash.

Comments

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.