I would like to split the below value:
var stringValue = "DEENFR";
As we see, there are 3 anchors hence I have tried
1. var result = stringValue.split("<a/>");
2. var myRegexp = new RegExp("<a/>");
var result = stringValue.split(myRegexp);
What would be the best regex or way to get these 3 anchors ?
here's a pure regex solution:
var stringValue = 'DEENFR';
var ar = stringValue.match(/<a.*?<\/a>/g)
console.log(ar)
You can use positive look ahead assertion to get the splitting position
var stringValue = 'DEENFR';
var res = stringValue.split(/(?=<a.*?<\/a>)/);
console.log(res);
Regex explanation
You are already there, just one extra step (okay, maybe 2) is required
var finalArray = stringValue.split("</a>").filter( function(val){
return val.length > 0; //filter out empty results
}).map( function(val){
return val + "</a>"; //append "</a>" to the rest of the results
})
First your string contains double quotes (") you should either escape them or encapsulate it in single quotee (').
var stringValue = 'DEENFR';
var result = stringValue.split("</a>")
.filter(function(item) {
return item ? true : false;
})
.map(function(item) {
return item + '</a>';
});
Related
I would like to know how can I remove the First word in the string using JavaScript?
For example, the string is "mod1"
I want to remove mod..I need to display 1
var $checked = $('.dd-list').find('.ModuleUserViews:checked');
var modulesIDS = [];
$checked.each(function (index) { modulesIDS.push($(this).attr("id")); })
You can just use the substring method. The following will give the last character of the string.
var id = "mod1"
var result = id.substring(id.length - 1, id.length);
console.log(result)
Try this.
var arr = ["mod1"];
var replaced= $.map( arr, function( a ) {
return a.replace("mod", "");
});
console.log(replaced);
If you want to remove all letters and keep only the numbers in the string, you can use a regex match.
var str = "mod125lol";
var nums = str.match(/\d/g).join('');
console.log(nums);
// "125"
If you don't want to split the string (faster, less memory consumed), you can use indexOf() with substr():
var id = "mod1"
var result = id.substr(id.indexOf(" ") -0);
console.log(result)
Hey I want a function that can split a string for example "(12/x+3)*heyo" which i could edit each number, letter and word by itself and then return the edited version. So far i got this (which not work as intended):
function calculate(input){
var vars = input.split(/[+-/*()]/);
var operations = input.split(/[^+-/*()]/);
var output = "";
vars = vars.map(x=>{
return x+"1";
});
for(var i=0; i<operations.length; i++){
output += operations[i]+""+((vars[i])?vars[i]:"");
}
return output;
}
For example: (12/x+3)*heyo returns: (1121/x1+31)*1heyo1 but should return (121/x1+31)*heyo1
You can use regex and replace method for this task:
var s = "(12/x+3)*heyo";
console.log(
s.replace(/([a-zA-Z0-9]+)/g, "$1" + 1)
)
Depending what characters you want to match, you may want /([^-+/*()]+)/g as the pattern:
var s = "(12/x+3)*heyo";
console.log(
s.replace(/([^-+/*()]+)/g, "$1" + 1)
)
It looks like the vars array is populated with empty results, which are adding "1" inadvertently. I slightly modified your arrow function to check x for a value.
vars = vars.map(x=>{
if (x) {
return x+"1";
}
});
It can be simplified a bit (but \w matches underscore too [a-zA-Z0-9_]) :
console.log( '(12/x+3)*heyo'.replace(/\w+/g, '$&1') )
console.log( '(12/x+3)*heyo'.replace(/\w+/g, m => m + 1) )
I have a chain like this of get page
file.php?Valor1=one&Valor2=two&Valor3=three
I would like to be able to delete the get request parameter with only having the value of it. for example , remove two
Result
file.php?Valor1=one&Valor3=three
Try with
stringvalue.replace(new RegExp(value+"[(&||\s)]"),'');
Here's a regular expression that matches an ampersand (&), followed by a series of characters that are not equals signs ([^=]+), an equals sign (=), the literal value two and either the next ampersand or the end of line (&|$):
/&[^=]+=two(&|$)/
let input = 'file.php?&Valor1=one&Valor2=two&Valor3=three';
let output = input.replace(/&[^=]+=two/, '');
console.log(output);
If you're getting the value to be removed from a variable:
let two = 'two';
let re = RegExp('&[^=]+=' + two + '(&|$)');
let input = 'file.php?&Valor1=one&Valor2=two&Valor3=three';
let output = input.replace(re, '');
console.log(output);
In this case, you need to make sure that your variable value does not contain any characters that have special meaning in regular expressions. If that's the case, you need to properly escape them.
Update
To address the input string in the updated question (no ampersand before first parameter):
let one = 'one';
let re = RegExp('([?&])[^=]+=' + one + '(&?|$)');
let input = 'file.php?Valor1=one&Valor2=two&Valor3=three';
let output = input.replace(re, '$1');
console.log(output);
You can use RegExp constructor, RegExp, template literal &[a-zA-Z]+\\d+=(?=${remove})${remove}) to match "&" followed by "a-z", "A-Z", followed by one or more digits followed by "", followed by matching value to pass to .replace()
var str = "file.php?&Valor1=one&Valor2=two&Valor3=three";
var re = function(not) {
return new RegExp(`&[a-zA-Z]+\\d+=(?=${not})${not}`)
}
var remove = "two";
var res = str.replace(re(remove), "");
console.log(res);
var remove = "one";
var res = str.replace(re(remove), "");
console.log(res);
var remove = "three";
var res = str.replace(re(remove), "");
console.log(res);
I think a much cleaner solution would be to use the URLSearchParams api
var paramsString = "Valor1=one&Valor2=two&Valor3=three"
var searchParams = new URLSearchParams(paramsString);
//Iterate the search parameters.
//Each element will be [key, value]
for (let p of searchParams) {
if (p[1] == "two") {
searchParams.delete(p[0]);
}
}
console.log(searchParams.toString()); //Valor1=one&Valor3=three
i have comma separated string like
var test = 1,3,4,5,6,
i want to remove particular character from this string using java script
can anyone suggests me?
JavaScript strings provide you with replace method which takes as a parameter a string of which the first instance is replaced or a RegEx, which if being global, replaces all instances.
Example:
var str = 'aba';
str.replace('a', ''); // results in 'ba'
str.replace(/a/g, ''); // results in 'b'
If you alert str - you will get back the same original string cause strings are immutable.
You will need to assign it back to the string :
str = str.replace('a', '');
Use replace and if you want to remove multiple occurrence of the character use
replace like this
var test = "1,3,4,5,6,";
var newTest = test.replace(/,/g, '-');
here newTest will became "1-3-4-5-6-"
you can make use of JavaScript replace() Method
var str="Visit Microsoft!";
var n=str.replace("Microsoft","My Blog");
var test = '1,3,4,5,6';
//to remove character
document.write(test.replace(/,/g, ''));
//to remove number
function removeNum(string, val){
var arr = string.split(',');
for(var i in arr){
if(arr[i] == val){
arr.splice(i, 1);
i--;
}
}
return arr.join(',');
}
var str = removeNum(test,3);
document.write(str); // output 1,4,5,6
You can also
var test1 = test.split(',');
delete test1[2];
var test2 = test1.toString();
Have fun :)
you can split the string by comma into an array and then remove the particular element [character or number or even string] from that array. once the element(s) removed, you can join the elements in the array into a string again
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
You can use this function
function removeComma(inputNumber,char='') {
return inputNumber.replace(/,/g, char);
}
Update
function removeComma(inputNumber) {
inputNumber = inputNumber.toString();
return Number(inputNumber.replace(/,/g, ''));
}
I have following INPUT out.
pieChart.js
stackedColumnChart.js
table.js
and i want OUTPUT like that(wanna remove .js from )
pieChart
stackedColumnChart
table
var array = ['pieChart.js', 'stackedColumnChart.js', 'table.js'];
var modifiedArray = array.map(function(el) {
return el.replace('.js', '');
});
console.log(modifiedArray);
If input is a multi-line string:
var input = "pieChart.js\n" +
"stackedColumnChart.js\n" +
"table.js";
var output = input.replace(/\.js$/mg, '');
If it's an array:
var input = ["pieChart.js","stackedColumnChart.js","table.js"];
var output = $.map(input, function(el){
return el.replace(/\.js$/, '');
});
You can loop through the strings and take substring of those strings.
In this case :
var array = ['pieChart.js', 'stackedColumnChart.js', 'table.js'];
for (item in array){
newItem = item.substr(0, item.length-3);
console.log(newItem);
}
You just substring the characters except the last 3
var dotJS = "pieChart.js";
var withoutJS = dotJS.substr(0,dotJS.length-3);
alert (withoutJS);
Now you have a string minus those last three characters.
(pffft... Wow, I'm late with my answer here.)