Use multiple instructions in one RegExp(JS) - javascript

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

Related

JS regex replace not working

I've got a JS string
var str = '<at id="11:12345678">#robot</at> ping';
I need to remove this part of a string
<at id="11:12345678">#
So I am trying to use
var str = str.replace("<at.+#","");
But there is no change after excution. Moreover if I try to use match it gives me
str.match("<at.+#");
//Result from Chrome console Repl
["<at id="11:12345678">#", index: 0, input: "<at id="11:12345678">#robot</at> ping"]
So pattern actualy works but replace do nothing
Use // for regex. Replace "<at.+#" with /<at.+#/.
var str = '<at id="11:12345678">#robot</at> ping';
str = str.replace(/<at.+#/,"");
console.log(str);
Documentation for replace

How to remove exact word from a string

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

multiple occurence in a string

I have a string in which I want to replace an occurence which I am not being able to achieve following is the code
var code="user_1/has some text and user_1? also has some text";
newcode=code.replace(/user_1//g,'_');
One more thing if i have to replace a string from another string how to do?
example.
var replacestring="user_1";
var code="user_1/some value here for some user";
var newcode=code.replace(/+replacestring+/g,'_');
/ is a special char thus needs to be escaped with \ before it:
var code = "user_1/has some text and user_1? also has some text";
var newcode = code.replace(/user_1\//g, '_');
alert(newcode);​
Live DEMO
if you want to replace all user_1, use this:
var code = "user_1/has some text and user_1? also has some text";
var newcode = code.replace(/user_1/g, '_');
alert(newcode);​
Live DEMO
Escape the / in the regex using \
newcode=code.replace(/user_1\//g,'_');
For your comment
#Vega I have another confusion. can i use a value in a string to pass
instead of user_1/ for replacement? what would be the syntax?
You can initialize RegEx object like below,
var userName = 'user_1/';
var newcode = code.replace(new RegExp(userName, 'g'), '_');
Read More about RegEx

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