-2

I am trying to replace a string as text is being inputted in the text input, the strings are {b} and {\b}, but it doesn't replace all the strings, it leaves some behind, This is the function below

 <TextInput
     value={text}
     onSelectionChange={this.onSelectionChange}
     onChangeText={text => this.inputer(text)}
     underlineColorAndroid={"transparent"}
     placeholder="Your Note..."
     multiline={true}
     selectionColor={"yellow"}
     ref={input => {this.TextInput = input;}}
     onBlur={() => this.setState({ pressed: false })}
     placeholderStyle={{ fontSize: 15, fontFamily: "zsMedium" }}
     placeholderTextColor="rgba(0, 0, 0, 0.2)"
     style={{
         paddingLeft: 28,
         paddingTop: 28,
         paddingRight: 28,
         width: "100%",
         backgroundColor: "transparent",
         fontSize: 15,
         fontFamily: "zsMedium",
         color: "black"
     }}
 />

 inputer(text) {
     this.setState({ text: text });
     this.setState({ boldButton: false });
     const someString = text;
     anotherString = someString.replace(/{b}/, "");
     console.log(anotherString + "{b}");
     anotherString = anotherString.replace(/{\/b}/, "");
     console.log(anotherString + "{/b}");
     this.setState({ text2: anotherString });
  }

The original text is {b}Gt{/b} jjj {b}fff{/b}{b}feew ggt{/b}, For console.log(anotherString + "{b}"); i.e after replacing {b} the text becomes Gt{/b} jjj {b}fff{/b}{b}feew ggt{/b}, after replacing {/b} the text becomes Gt jjj {b}fff{/b}{b}feew ggt{/b} all the strings are not replaced, please what may be wrong

huaLin
  • 97
  • 2
  • 13
Adokiye Iruene
  • 740
  • 2
  • 10
  • 34

1 Answers1

3

The global flag g is missing and as a better approach use the following regex for capturing the desired strings /{b}|{\/b}/g.

console.log("{b}Gt{/b} jjj {b}fff{/b}{b}feew ggt{/b}".replace(/{b}|{\/b}/g, ""));
Ele
  • 33,468
  • 7
  • 37
  • 75