0

I have a question. I want to build a regular expression in java script within a loop. I have the string "hi!whats.up" and I want to select in the loop each substring ("hi!","whats","up"), with a regular expression.

Thank you in advance.

3
  • Please consider sharing some code that you've tried already Commented Nov 14, 2018 at 12:58
  • @Tester thx for your answer. I have no code yet and don't want have the whole code from anybody. I just want to understand the regular expression of this example. I know that I can use " [.].* " But this just takes the substring ".up". And I want to have all three pieces in the regex. Commented Nov 14, 2018 at 13:03
  • Do you want to include the ! with the preceding word but not other special characters? Commented Nov 14, 2018 at 13:12

2 Answers 2

1

You want to use .split() together with a regular expression for symbols.

Something like

"hi!whats.up".split(/[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]/)
    .forEach(function (word) {
        console.log(word);
})
Sign up to request clarification or add additional context in comments.

3 Comments

thank you. So is "word" my variable? Like var String word="hi!whats.up" ?
when I compile that I get an error at the (/[-!$%^&*()_+|~=`{}[]:";'<>?,.\/]/)
No, that's just for logging. The hardcoded "hi!whats.up" is what's being split
1
let [a, b, c] = 'hi!whats.up'.split (/[^\w]+/)

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.