简体   繁体   中英

How can I replace string between digits?

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.

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.

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(',')

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

您是否尝试过保存replace()的返回值?

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

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.

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

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); 

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

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

 var value = "12345XXXX98765XXXX"; value = value.split("XXXX"); console.log(value); 

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)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM