var str = " Hello World! ";
str.trim();
Where str is the regex matched string.
I know but i need to do it inside regex expression because I then need to use '\1' inside regex to match it to last referenced!
/word\s*([\w ]*\w)\s*word/i
This ensure that the last char is always a word character.
Is your example reflective of your actual use case?
Do you have clear delimiters that would allow you to accurately determine the substring that you want?
The actual string is:
<STATIC MUST OPTIONAL1 : OPTIONAL2>
all kind of character inside here!!
</STATIC MUST OPTIONAL1>
Static is a static known word.
Must is a required word (only 1 word).
Optional1 is a alpha numerical multiword (optional) group.
Optional2 is a alpha numerical multiword (optional) group.
the ':' are optional as well if there is no OPTIONAL2.
I want to capture the MUST, OPTIONAL1 (is exists) and OPTIONAL2 (if exists) and (all the characters inside here!!)
What exactly do you want to capture? Only the characters inside the tag or (one or more of) MUST, OPTIONAL1, OPTIONAL2, also?
This is my first try:
/<(STATIC ([^ ]+)(?: ([\w ]+))?)(?: : ([\w ]+))>([^<]+)<\/\1>/
It captures the following groups:
0: The whole tag.
1: Everything from STATIC to OPTIONAL 1 (for back reference)
2: MUST
3. OPTIONAL1
4: OPTIONAL2
5: Tag content
It only matches a fixed number of spaces (1 between each captured group and before/after the ":"). If you want that to be more flexible, the regex would need to be modified a bit.
/edit: The above isn't fully working (fails, when optional groups are missing). I'm working on it.
i use something like in my basic module for the very same porpouse:
/<TAGNAME>((?:[^<]|<[^\/])*)<\/TAGNAME>/gi
It gets any value inside of the tag, it only stops at the sequence '</' it allows to use anything inside of the tag, including '<' and '\' as long they aren't both in sequence.
In this case, you could use this, instead:
[\s\S]*?
This will match every character, over multiple lines, until the end tag is encountered. It will also match closing tags, but since its lazy, it will stop as soon as the end tag (in this case </TAGNAME>) is encountered.