2

I want convert number to * and add space after every 4 digits. ex: 12345678 -> 1234 5678 -> **** **** Now i just convert number to * but can't add space . ( when convert number to * , add space function not work ) Please help me!

import React, { Component } from 'react';
import { Text, TextInput, View } from 'react-native';

export default class PizzaTranslator extends Component {
  constructor(props) {
    super(props);
    this.state = {text: ''};
  }

  render() {
    return (
      <View style={{padding: 10}}>
        <TextInput
          style={{height: 40}}
          placeholder="Type here to translate!"
          onChangeText={(text) => this.setState({text})}
          value={this.state.text}
        />
        <Text style={{padding: 10, fontSize: 42}}>
{this.state.text.replace(/(\(-?\d+(?:\.\d*){4})/g,'$1').replace(/(^\s+|\s+$)/,'') .slice(20).padStart(this.state.text.length, '*')}
        </Text>
      </View>
    );
  }
}

i expect input : 123456789123 output: **** **** ****

Zem Zem
  • 139
  • 1
  • 11
  • You said you succeeded to convert the number to `*` but can't add space. So your issue is exactly like the one above. –  Oct 24 '19 at 15:32
  • yes i can convert number to * . Like 12345678 -> ******** . but when it's **** , i can't add space . Add space just work with a->Z or 0 -> 9 . not work with * – Zem Zem Oct 24 '19 at 15:36
  • Then add the function _before_ converting to `**** ****`. –  Oct 24 '19 at 15:38

3 Answers3

2

One line Answer:

"123456781234".replace(/[0-9]/g, "*").match(/.{1,4}/g).join(" ");
Channa
  • 3,267
  • 7
  • 41
  • 67
  • i work with textInput so when start TextInput = '' https://snack.expo.io/SJTEASkcB?session_id=snack-session-nMkyeVZjL – Zem Zem Oct 24 '19 at 16:15
1

Try this for grouping

this.state.text.replace(/[^\dA-Z]/g, '').replace(/(.{4})/g, '$1 ').trim();

Edit: how to split '********' in javascript

var str = "************";
var chuncks = str.match(/.{1,4}/g);
var new_value = chuncks.join(" "); //returns **** **** ****

Following will be the complete answer after combining the above two:

var starString = "123456781234".replace(/[0-9]/g, "*");
var chuncks = starString.match(/.{1,4}/g);
var new_value = chuncks.join(" "); //returns **** **** ****
Channa
  • 3,267
  • 7
  • 41
  • 67
  • it's the same replace(/(\(-?\d+(?:\.\d*){4})/g,'$1').replace(/(^\s+|\s+$)/,'') but both not work when convert to **** – Zem Zem Oct 24 '19 at 15:31
  • see my edit on how to split – Channa Oct 24 '19 at 15:39
  • 1
    it's TextInput so when str = '' -> chuncks will be null . ``` export function convertString(text) { var str = (text).slice(20).padStart(text.length, '*') var chuncks = str.match(/.{1,4}/g); if (chuncks === null || chuncks === undefined || chuncks === '') { chuncks = ["*"] } else { var new_value = chuncks.join(" "); } return new_value } ``` i follow your code and solved by this way . Thank you – Zem Zem Oct 24 '19 at 16:09
0

You could just go oldskool and use a loop. :-)

const num = 123456789123456789;
const preStr = num.toString();
let postStr = "";

for(var i = 0; i < preStr.length; i++){
    postStr += '*';
    i % 4 === 3 && (postStr += ' ');
}
console.log(postStr)
// output: **** **** **** **** **

As a function:

const format = input => {
    let output = "";
    for(var i = 0; i < preStr.length; i++){
        output += '*';
        i % 4 === 3 && (output += ' ');
    }
    return output;
}
console.log(format("123456789123456789"));

// output: **** **** **** **** **
James Trickey
  • 1,322
  • 13
  • 21