How to remove exact word from a string - javascript

If I have a string
TestString = "{Item:ABC, Item:DEF, Item:GHI}";
How can I remove all of the "Item:"s.
I have tried to use
msg = TestString.replace(/[\Item:/&]+/g, "");
but this unfortunately removes all the Is, Ts. Es and M,s from any letters that may follow.
How can I remove the exact text
Thanks!

It should be simple, you can directly create a Regex like /Item:/g,
var TestString = "{Item:ABC, Item:DEF, Item:GHI}";
var msg = TestString.replace(/Item:/g, "");
console.log(msg);

You could use
/Item:/g
for replacing Item:.
The fomer regular expression
/[\Item:/&]+/g
used a character class with single letters instead of a string.
var TestString = "{Item:ABC, Item:DEF, Item:GHI}",
msg = TestString.replace(/Item:/g, "");
console.log(msg);

Related

Use multiple instructions in one RegExp(JS)

I have:
var myText = <"input">
I want to cut the string down to only input. Is possible to define a regular expression, which does this? I know how to get rid of the <"
myText = myText.replace(/<"/,g,'')
But what about the end of the line? Of course I could just write another regular expression, like this:
myText = myText.replace(/<"/,g,'').replace(/">/,g,'')
But I´m sure there is an easier way, right? :)
Use regexp that matches start or end of sequence
var myText = "<input>";
myText.replace(/^<"|">$/g, '')
Instead you can use .match() method:
var myText = "<input>";
console.log(myText.match(/[(a-z)]+/g)[0]);
You can do like this:
var myText = '<input>';
myText.replace(/<(.*)>/, '$1');
Or
myText.match(/<(.*)>/)[1]
You can use a character class [] to specify any of the present characters:
var myText = '<input>';
console.log(myText.replace(/[<>]/g, ''));
If you want to replace any non alphanumeric letters, you can do:
var myText = '<input>';
console.log(myText.replace(/[^a-zA-Z0-9]/g, ''));
var myText = "<input>";
console.log(myText.replace(/(^<)|(>$)/g, '');

how to combine regEx expressions

How do I take each of these and combine them into a single regEx expression?
var t = "<test>";
t = t.replace(/^</, '');
t = t.replace(/>+$/, '');
which results in t equal to test without the <>
You can use pipe |. In regex it means OR:
t = "<test>>".replace(/^<|>+$/g, "");
// "test"
But of course, you can use another ways like:
t = "<test>>".replace(/[<>]/g, "");
// "test"
or even with match:
t = "<test>>".match(/\w+/)[0];
// "test"
Make sure you've added the g-global flag when needed. This flag stands for all occurrences.
If I understand correctly you only want to replace beginning '<' symbols and ending '>' symbols, try this one [>$^<]
var t = "<test>";
t = t.replace('/[>$^<]/', '');
Try this
t = t.replace(/^</, '').replace(/>+$/, '');
If you want it in single line, use | to combine the regex.. Like this
t = t.replace(/^<|>+$/g, "");
or capture groups like this
t = t.replace(/^<(.*)>+$/g, '$1');

how to config RegExp when string contains parentheses

I'm sure this is an easy one, but I can't find it on the net.
This code:
var new_html = "foo and bar(arg)";
var bad_string = "bar(arg)";
var regex = new RegExp(bad_string, "igm");
var bad_start = new_html.search(regex);
sets bad_start to -1 (not found). If I remove the (arg), it runs as expected (bad_start == 8). Is there something I can do to make the (very handy) "new Regexp" syntax work, or do I have to find another way? This example is trivial, but in the real app it would be doing global search and replace, so I need the regex and the "g". Or do I?
TIA
Escape the brackets by double back slashes \\. Try this.
var new_html = "foo and bar(arg)";
var bad_string = "bar\\(arg\\)";
var regex = new RegExp(bad_string, "igm");
var bad_start = new_html.search(regex);
Demo
Your RegEx definition string should be:
var bad_string = "bar\\(arg\\)";
Special characters need to be escaped when using RegEx, and because you are building the RegEx in a string you need to escape your escape character :P
http://www.regular-expressions.info/characters.html
You need to escape the special characters contained in string you are creating your Regex from. For example, define this function:
function escapeRegex(string) {
return string.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&');
}
And use it to assign the result to your bad_string variable:
let bad_string = "bar(arg)"
bad_string = escapeRegex(bad_string)
// You can now use the string to create the Regex :v:

Javascript replace does not replace

The pattern in this code does not replace the parenthesis. I've also tried "/(|)/g".
var re = "/[^a-z]/g",
txt = navsel.options[i].text.split(" ")[0], // here I get the text from a select and I split it.
// What I expect is strings like "(en)" , "(el)" etc
txt = txt.replace(re," ")
Thanks in advance
Your regex is a string, this will try to replace that exact string. Regex objects don't have quotes around them, just the delimiters. Try it like this:
var re = /[^a-z]/g,
txt = navsel.options[i].text.split(" ")[0], // here I get the text from a select and I split it.
txt = txt.replace(re," ");
Or if you prefer strings (and a more explicit type):
var re = new RegExp("[^a-z]", "g")

how to extract string part and ignore number in jquery?

I have a string like foobar1, foobaz2, barbar23, nobar100 I want only foobar, foobaz, barbar, nobar and ignoring the number part.
If you want to strip out things that are digits, a regex can do that for you:
var s = "foobar1";
s = s.replace(/\d/g, "");
alert(s);
// "foobar"
(\d is the regex class for "digit". We're replacing them with nothing.)
Note that as given, it will remove any digit anywhere in the string.
This can be done in JavaScript:
/^[^\d]+/.exec("foobar1")[0]
This will return all characters from the beginning of string until a number is found.
var str = 'foobar1, foobaz2, barbar23, nobar100';
console.log(str.replace(/\d/g, ''));
Find some more information about regular expressions in javascript...
This should do what you want:
var re = /[0-9]*/g;
var newvalue= oldvalue.replace(re,"");
This replaces al numbers in the entire string. If you only want to remove at the end then use this:
var re = /[0-9]*$/g;
I don't know how to do that in JQuery, but in JavaScript you can just use a regular expression string replace.
var yourString = "foobar1, foobaz2, barbar23, nobar100";
var yourStringMinusDigits = yourString.replace(/\d/g,"");

Categories

Resources