0

I have a string like this:

const value = "12345XXXX98765XXXX"

I want to remove 'XXXX' and add ',' instead of 'XXXX'. How can I do that?

My expected value is : "12345,98765". And then I want an array with elements of value. My array should be like this:

var array = []; array[0] = "12345", array[1] = "98765"

How can I do that?

I try this code: value.replace("XXXX", ",") but it shows XXXX again.

willywonka15
  • 97
  • 10
  • 1
    `.replace()` doesn't modify the original string. It works on a copy and returns the changed version – j08691 Apr 03 '19 at 12:49
  • `value.match(/\d+/g)`? – Wiktor Stribiżew Apr 03 '19 at 12:52
  • 1
    Possible duplicate of [How to replace all occurrences of a string in JavaScript](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) – Scoots Apr 03 '19 at 12:54
  • Also: https://stackoverflow.com/questions/96428/how-do-i-split-a-string-breaking-at-a-particular-character – Scoots Apr 03 '19 at 12:54

10 Answers10

1

Try this:

const value = "12345XXXX98765XXXX"

const result = value
  .replace(/[^\d]+/g, ',') // replace any consequent non-digit symbols with single comma
  .replace(/,$/, '') // trim off the last comma if any
  
console.info("String: " + result)

// or if you are looking to obtain the array directly, you can do:

const arr = value
  .split(/[^\d]+/) // split on consequent non-digit symbols
  .filter(Boolean) // filter out empty items
  
console.info("Array: ", arr)

Notice that the above snippets will work no matter if you have X as placeholder or any other letter and number of them (4 or more or less) also doesn't matter.

jayarjo
  • 16,124
  • 24
  • 94
  • 138
1

For sneak peeking proposals, The full code is here:

var value = "12345XXXX98765XXXX"
for(var i = 0; i < value.length; i++) {
  if(value[i] === 'X') {
    value = value.replace('XXXX', ',')
  }
}
value = value.substring(0, value.length - 1)
var parts = value.split(',')
console.info(parts)

If you want to know more about each part, you can read below:

I want to remove 'XXXX' and add ',' instead of 'XXXX'. How can I do that?

First of all, you need to use something called replace(), but how you want to remove all the occurrences of 'XXXX', you can do a for loop searching for any 'X' you could find.

reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

var value = "12345XXXX98765XXXX"
for(var i = 0; i < value.length; i++) { //looking through the whole string
    if(value[i] === 'X') { //For any X found
        value = value.replace('XXXX', ',') //you should replace them for a comma
    }
}

As you might have noticed, the last 'XXXX' also became a comma, so to remove it just use substring().

reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring

value = value.substring(0, value.length - 1)

So, now you have a string just like you wanted. "12345,98765"

For splitting and getting the values that are before and after the comma, you can use split() and save the result in a new variable, which is an array.

reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split

var parts = value.split(',')
0

If you use replace it only changes one time you have to use a regex instead

value.replace(/XXX/g, ",")

Later you can use split to convert that string into an array https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split

0

Have you tried saving the return of replace()?

array = value.replace("XXXX",",");

Eddie
  • 35
  • 1
  • 7
0

We can try a regex replacement, followed by a string split:

var value = "12345XXXX98765XXXX"
value = value.replace(/XXXX/mg, ",").replace(/,$/, "");
parts = value.split(",");
console.log(parts);

Note that the second regex replacement conditionally removes a final trailing comma, should the string happen to end in XXXX (which in your example, it does). Should no trailing comma be present, the second replacement would just no-op.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

You need to reassign the return value of replace

value = value.replace("XXXX",",");

Also since you declared it as a const you won't be able to reassign so use a let instead

BugCatcherJoe
  • 487
  • 3
  • 17
0

You can use replace() with regex for the matching tokens (XXXX):

const value = "12345XXXX98765XXX";
var res = value.replace(/[X]+/g, ',').replace(/,$/, '').split(',');
console.log(res);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

.replace() returns a new string (it does not modify the existing string). You need to do the following:

value = value.replace("XXXX", ",")

Diogo Peres
  • 1,302
  • 1
  • 11
  • 20
0

var value = "12345XXXX98765XXXX";

value = value.split("XXXX");

console.log(value);
Kiran Dash
  • 4,816
  • 12
  • 53
  • 84
0

Since you want an array, the obvious way would be to use split instead of replace

const value = "12345XXXX98765XXXX"
console.log(value.split('XXXX', 2))
baao
  • 71,625
  • 17
  • 143
  • 203