Home>

I want to do something like this with javascript.

Example:
input ="exampleOh [bb + cc] Yes [U.s]: test"

output [0] ="example ah"
output [1] ="[bb + cc]"
output [2] ="Good"
output [3] ="[う .s]"
output [4] =": test"

What I want to do is to make an array in order of what is enclosed in [] and what is not.
If i have any idea, please give me a professor.

Supplement 1:
The purpose is to replace the characters surrounded by [] without input with complicated rules.

Example:
"[bb + cc]"⇒"BC"
"[U.s]"⇒"uu"

"exampleOh [bb + cc] Nice [U.s]: test"
⇒"example Oh bc good uu: test"

** Addition ****
Example:
"[date]"⇒Current date time
"[rand: 1 ~ 10]"⇒A random integer between 1 and 10
"[yourname]"⇒Calls the saved character string corresponding to the keyword "yourname" (the contents of the character string may be changed when the page transitions. No conversion if there is no corresponding character string)
"[array: A, 4]"⇒The fourth character of the saved data [array: A]. No conversion if [array: A] does not exist
We plan to implement such a variety of rules,
The rules are not constant and are intended for site visitors to change, add, or delete.
******

In order to achieve this, I came to the question with the expectation that array partitioning with regular expressions would be reasonable.

Supplement 2:
[Ah!]
If parentheses are confused like this,
"[Ah!"
"[Ah]"
It doesn't matter which one you use to extract.
It is okay if they are unified.
** Supplementary notes **
However,
"[Ai]"and"[I]"
I want to avoid reading the same character position more than once.
******

We are looking forward to your response.<(_ _)>

  • Answer # 1

    How about this?

    output = input.split (/(\[.*?\])/);

  • Answer # 2

    No need to split I think.

    const myMap = new Map ([['[bb + cc]', 'BC'], ['[U.s]', 'uu']]);
    let ex = 'example ah [bb + cc] okay [ou.s]: test';
    myMap.forEach ((value, key) =>{
        console.log (key + '=' + value);
        ex = ex.replace (key, value);
    });
    console.log (ex);// example ah bc good uu: test

    Working sample:https://jsfiddle.net/q3tdwgvm/


    【Map-JavaScript | MDN】
    https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map

    [String.prototype.replace ()-JavaScript | MDN]
    https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace

  • Answer # 3

    I'm sorry, but I would like to see only the flow of conversion as well.
    The question that is divided into arrays in [] and the replacement question in "complex rules" in [] are easier to answer if they are divided as separate questions.

    Output result
    import java.util.LinkedList;
    public class memo203 {
        // input = "example oh [bb + cc] nice [ou.s]: test"
        // ↓
        // output [0] = "example aa"
        // output [1] = "[bb + cc]"
        // output [2] = "OK"
        // output [3] = "[Uh.s]"
        // output [4] = ": test"
        / **
         * Input/output
         * /
        public static void main (String [] args) {
            String [] input = new String [3];
            String [] output;
            input [0] = "012 [45] 789 [ab]";
            output = func (input [0]);
            for (String str: output) {
                System.out.println (str);
            }
            System.out.println ();
            input [1] = "012 [4 [5] 7] 89 [ab]";
            output = func (input [1]);
            for (String str: output) {
                System.out.println (str);
            }
            System.out.println ();
            input [2] = "example ah [bb + cc] nice [ou.s]: test";
            output = func (input [2]);
            for (String str: output) {
                System.out.println (str);
            }
            System.out.println ();
        }
        / **
         * Convert string to array
         * /
        public static String [] func (String str) {
            LinkedList<String>list = new LinkedList<String>();
            StringBuilder sb = new StringBuilder ();
            sb.append (str);
            int s = 0;// start
            int e = 0;// end
            while (sb.length ()>1) {
                s = sb.indexOf ("[");
                if (s>0) {
                    list.add (sb.substring (0, s));
                    sb.replace (0, s, "");
                }
                int cntL = 0;
                int cntR = 0;
                if (! (sb.indexOf ("[")<0)) {
                    for (int i = 0;i<sb.length ();i ++) {
                        if (sb.charAt (i) == '[') {
                            cntL ++;
                        }
                        if (sb.charAt (i) == ']') {
                            cntR ++;
                        }
                        if (cntL == cntR&&cntR>0) {
                            e = i;
                            break;
                        }
                    }
                    list.add (sb.substring (0, e + 1));
                    sb.delete (0, e + 1);
                } else {
                    list.add (sb.substring (0));
                    sb.delete (0, sb.length ()-1);
                }
            }
            System.out.println (list);
            String [] result = list.toArray (new String [list.size ()]);
            return result;
        }
    }