How to delete all words with "-" before in Javascript [duplicate] - javascript

This question already has answers here:
Regex, replace all words starting with #
(5 answers)
Closed 5 years ago.
I'm totally new to regex and can't figure out how to realize it via Javascript.
For example, I have the string var string = "-just an example -string for stackoverflow". The expected result is string = "an example for stackoverflow".
Thanks in advance

Try this:
-\w+\s+
and replace by empty
Regex Demo
const regex = /-\w+\s+/gm;
const str = `-just an example -string for stackoverflow`;
const subst = ``;
const result = str.replace(regex, subst);
console.log(result);

Try this with simple forEach.
var string = "-just an example -string for stackoverflow";
var strArray = string.split(' ');
strArray.forEach(function(value, i){
if(value.startsWith('-')){
strArray.splice( i, 1 )
}
});
console.log(strArray.join(' '));

Related

How to create a function to split the following string? [duplicate]

This question already has answers here:
What is the shortest function for reading a cookie by name in JavaScript?
(20 answers)
Closed 2 years ago.
I have this string
"G_ENABLED_IDPS=app; COOKIE_EINF=someCookie; _ga=someGA;
_hjid=someHJID; _gcl_au=someglcau; COOKIE_EINF_SESS=somecookie1; _gid=somegid; _hjIncludedInPageviewSample=2; _hjTLDTest=3; _hjAbsoluteSessionInProgress=0; _hjIncludedInSessionSample=1; _gat_UA-124355-12=5"
And i need some sort of function to split this string given an argument , for example given that my string is text
text.split(";") , will split it into an array separating it by ";"
But i need a function like this
returnText(text , property) that would work like
returnText(text, "_gcl_au") --> returns "someglcau"
You could actually use a regex replacement approach here, for a one-liner option:
function returnText(text, property) {
var term = text.replace(new RegExp("^.*\\b" + property + "=([^;]+)\\b.*$", "gm"), "$1");
return term;
}
var input = "G_ENABLED_IDPS=app; COOKIE_EINF=someCookie;_ga=someGA;_hjid=someHJID; _gcl_au=someglcau; COOKIE_EINF_SESS=somecookie1; _gid=somegid; _hjIncludedInPageviewSample=2; _hjTLDTest=3; _hjAbsoluteSessionInProgress=0; _hjIncludedInSessionSample=1; _gat_UA-124355-12=5";
console.log(returnText(input, "_gcl_au"));
you can use split, just as you tried:
function returnText(text , property){
entries = text.split('; ');
const newEntries = [];
entries.forEach(item => {
let vals = item.split('=');
newEntries[vals[0]] = vals[1]
});
return newEntries[property];
}
const text = "G_ENABLED_IDPS=app; COOKIE_EINF=someCookie; _ga=someGA;_hjid=someHJID; _gcl_au=someglcau; COOKIE_EINF_SESS=somecookie1; _gid=somegid; _hjIncludedInPageviewSample=2; _hjTLDTest=3; _hjAbsoluteSessionInProgress=0; _hjIncludedInSessionSample=1; _gat_UA-124355-12=5";
console.log(returnText(text,'_gcl_au'));

Variable in regular expression [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 2 years ago.
How can I replace the "#" with the constant “begins” in the regular expression
function matchAlltext() {
const str = document.getElementById('textinput').value;
const begins = document.getElementById('begins').value;
const regexp = new RegExp(/#\w+/ig);
let matchAll = str.matchAll(regexp);
matchAll = Array.from(matchAll);
document.getElementById('textoutput').value = matchAll;
}
I need to find all the words in the text starting with the character entered in the input field
Like this?
const str = "abc abc abc"
const begins = "a"
const regexp = new RegExp(begins + "\\w+","ig");
let matchAll = str.matchAll(regexp);
matchAll = Array.from(matchAll);
console.log(matchAll); //logs [["abc"], ["abc"], ["abc"]]

Regex replace with captured [duplicate]

This question already has answers here:
Using $0 to refer to entire match in Javascript's String.replace
(2 answers)
Closed 5 years ago.
I want replace all non alphanumeric character in the string by it self surrender for "[" and "]".
I tried this:
var text = "ab!#1b*. ef";
var regex = /\W/g;
var result = text.replace(regex, "[$0]");
console.log(result);
I was expecting to get:
ab[!][#]1b[*][.][ ]ef
But instead i get:
ab[$0][$0]1b[$0][$0][$0]ef
How can I do this using Javascript(node)?
You need to wrap the group in parentheses to assign it to $1, like this:
var text = "ab!#1b*. ef";
var regex = /(\W)/g;
var result = text.replace(regex, "[$1]");
console.log(result);

How can I do string replace in jquery [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 6 years ago.
I have this code
$("#title").keyup(function(){
var titleval = $("#title").val();
var res = titleval.replace(" ", "-");
$("#newsurl").val(res);
});
to replace spaces into dash to get URL like this
wordone-wordtow-wordthree
but i have problem with this code it's just replace first space like this
wordone-wordtow wordthree
How can i solve this problem
You need to do a global match, you can do this with a regex
var res = titleval.replace(/\s/g, "-");
Though String.prototype.replace does support having flags passed, this is deprecated in firefox and already doesn't work in chrome/v8.
Alternate method (if regex is not mandatory) could be to split and join
var res = titleval.split(" ").join("-");
or
var res = titleval.split(/\s+/).join("-");
Use regex with global flag
titleval.replace(/\s/g, "-");
try like this:
$("#title").keyup(function(){
var titleval = $("#title").val();
var res = titleval.replace(/\s+/g, '-');
$("#newsurl").val(res);
});

Replacing of string using javascript [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 9 years ago.
I want to replace string from "_tr9-9" character from a string and replace them with _id character
Here str is changes only _tr9-0 will be changes dynamically remining will be same
str=Test_User_tr9-0;
Ex:
function (str)
{
var obj=str
}
In C#
string str = "Test_user_tr9-9";
string str2 = str.Replace("_tr9-9", "_id");
In Javascript
var str = "Test_user_tr9-9";
var str2 = str.replace("_tr9-9", "_id");
Note that both in Javascript and C# strings are immutable objects, so the replace/Replace methods return a new modified string (technically in C# the Replace returns the original string if it doesn't find anything to replace)
In JavaScript:
var index = str.lastIndexOf("_");
var result = str.substring(0, index) + "_id";
jsFiddle: http://jsfiddle.net/fNZkG/
You can use replace
In javascript
var str = "Test_user_tr9-9 to Test_user_id";
str = str.replace('tr9-9','id');
In C#
var str = "Test_user_tr9-9";
var str = str.Replace("_tr9-9", "_id");

Categories

Resources