0

How to capture the following pattern using JavaScript regular expressions?

I would like to capture the first sequence of characters ending at a word boundary. The sequence length should be minimal and greater than N.

For example.

N = 6, input = "aa bb cc ddd ee"
result = "aa bb cc" // the 1st minimal sequence ending at a word boundary > 6

2 Answers 2

2

If I well understood try something like

var n   = 6, 
    str = "aa bb cc ddd ee",
    re  = new RegExp("^.{"+ (n+1) +"}.*?\\b");

str.match(re);  // "aa bb cc"
Sign up to request clarification or add additional context in comments.

Comments

1

This regex should work

"^.{" + (n+1) + ",}?\b"

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.