Regex for trimming?!

I am trying to create a regexp for this:


word many words here word


i want to extract the 'many words here' with out the beginning and end spaces.


i use

Code:
/word\s*([\w ]+)\s*word/i

If the sentence is 'word she got       word'


the match will have all the extra spaces :/ can't it be trimmed? Thank you for your help.
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.
What exactly do you want to capture? Only the characters inside the tag or (one or more of) MUST, OPTIONAL1, OPTIONAL2, also?

I think i got it:
 

Code:
/<STATIC\s+(\w+)(?:( [\w ]*\w))?\s*(?::\s*([^<]?))?\s*>(?:([^]*?)<\/STATIC \1\2>)?/i

Don't know if it is optimal though 🙂

By the way @Iavra I posted in the topic: "discussion on how to 'properly' code in RM MV" give it a look and tell me your personal opinion! 🙂
This works also, if optional groups are missing (unlike my previous version):

Code:
/<(STATIC ([^ ]+)(?: ([\w ]+))?)(?: : ([\w ]+))?>([^<]+)<\/\1>/
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.
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.

I will test this. Thanks for the tip.

Original thread: https://forums.rpgmakerweb.com/threads/regex-for-trimming.56589/