6

Hey. First question here, probably extremely lame, but I totally suck in regular expressions :(

I want to extract the text from a series of strings that always have only alphabetic characters before and after a hyphen:

string = "some-text"

I need to generate separate strings that include the text before AND after the hyphen. So for the example above I would need string1 = "some" and string2 = "text"

I found this and it works for the text before the hyphen, now I only need the regex for the one after the hyphen.

Thanks.

1 Answer 1

10

You don't need regex for that, you can just split it instead.

var myString = "some-text";
var splitWords =  myString.split("-");

splitWords[0] would then be "some", and splitWords[1] will be "text".

If you actually have to use regex for whatever reason though - the $ character marks the end of a string in regex, so -(.*)$ is a regex that will match everything after the first hyphen it finds till the end of the string. That could actually be simplified that to just -(.*) too, as the .* will match till the end of the string anyway.

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

1 Comment

I need to match from the last instance of a character - this is a solution: stackoverflow.com/a/8374980/845584

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.