0

I have a string like this:

#ud %+^'()=%^'K%J+^K$#££½6 u896%+& 547900

I want to capture this #ud in a first variable and anything else that comes after, in a second variable, but not including the first space.

PS: #ud can change to #u64, #d and etc.

How do I do it in a clean and simple way?

1

2 Answers 2

2

Try

/^(#[^ ]+) (.+)$/

I use the space character to find the end of the first part.

https://www.debuggex.com/r/VZcx0JLtfY4vNrzR

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

Comments

1

You can use a regex to split the string:

var re = /^(\S+)\s(.*)/; 
var str = '#ud %+^\'()=%^\'K%J+^K$#££½6 u896%+& 547900';
alert(str.split(re).filter(Boolean));

The /(\S+)\s(.*)/ regex will match first characters that are not spaces with (\S+) and capture it, then the space, and then it captures the rest of the string. Capturing groups are necessary to add the texts to the resulting array. And the .filter(Boolean) function removes empty items from the array.

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.