1

I've a text variable in the following format -

value1|34|value2|45|value3|67|value4|687|

Now I just have the text 'value3' with me and I've to remove that value along with its associated number from the above string. After removing it I've to get -

value1|34|value2|45|value4|687|

Note: The numbers in the pipelines are prefixed with its value string. Ex - Value|56|. So if I've to remove a value I've to remove it along with its number.

3 Answers 3

2

It looks like you could benefit from using something like JSON as a storage format instead.

'value1|34|value2|45|value3|67|value4|687|'.replace(/value3\|\d+\|/,'')
Sign up to request clarification or add additional context in comments.

Comments

1

You can

var s = "value1|34|value2|45|value3|0000|value4|687|";
var r = "value3";

s = s.replace(new RegExp("(?:^|\\|)" + r + "\\|\\d+"), "")

Includes start-guard (wont match xxxvalue3)

4 Comments

This is removing the pipeline before the value. For example, after you remove value3 I'm getting - value1|34|value2|45value4|687|
If the value has some special character like this - "Test: (Has- Characters)" it is not working. I'm not able to figure out which of the character is causing the issue.
I guess parentheses () is causing the issue. How to fix it?
Changed re; to use a string that contain characters that form part of re syntax you must escape it; stackoverflow.com/questions/3446170/…
1
var input = 'value1|34|value2|45|value3|67|value4|687|',
    remove = 'value3',
    result = input.replace(RegExp(remove + '\\|\\d+\\|'), '');
console.log(result); // 'value1|34|value2|45|value4|687|'

4 Comments

Not working :(. I'm still getting the same original string after replacing it.
If the value has some special character like this - "Test: (Has- Characters)" it is not working. I'm not able to figure out which of the character is causing the issue.
I guess parentheses () is causing the issue. How to fix it?
Replace \d (which only matches digits) with a character class that would match everything you want to match, e.g. [0-9a-zA-Z\(\)\s-], or simply use [^|].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.