How to insert character after second characters in string? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I add a character after the second characters in string?
Ex: I want this : 1700 to become this: 17:00.

The simplest way, in my opinion, is using substr to split the string and concatenate the other character in between. This will work no matter the length of the second part:
var str = "1700";
str.substr(0,2)+":"+str.substr(2);

this would do the magic:
"1700".replace(/(..)$/, ":$1")
alternative you can do something like look from behind in substr too like:
var string = "1700";
string = string.substr(0, string.length -2) + ":" + string.substr(-2, 2);
they both also work on something like: 900 wich will turn into 9:00
the regex and the substr line both do the same. if you want readability i'd consider the regex.

You could do this:
'1700'.match(/../g).join(':')
The following regexp accepts 3+ chars:
'700'.match(/^(.+)(..)$/).slice(1, 3).join(':') // "7:00"
Shortest and probably fastest solution:
s[0]+s[1]+":"+s[2]+s[3]

Without using regexp, you can use substr --
string = string.substr(0,2) + ":" + string.substr(2);

You can use substring. Consider the following:
string1 = "1700";
string2 = ":";
string1 = string1.substring(0,2) + string2 + string1.substring(2);
alert(string1);

Related

replace something in a string with something else in JS [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
First part:
I want to replace the spaces in a string to \n but the first space I want it to be replaced with a double like this: \n\n
var string = "this is something random “
Result needed:
var string = "this\n\nis\nsomething\nrandom\n"
Second part:
I want to replace the string quotes with backticks.
var string = "this\n\nis\nsomething\nrandom\n"
Result needed:
var string = `this\n\nis\nsomething\nrandom\n`
First Part
For what you want, you can just call a replace function to replace the first occurrence, then a replaceAll/regex to replace all others:
let input = "Hello World !"
function replaceFirstThenAll(string, lookfor, replace) {
return string
// replace the first occurance
.replace(lookfor, replace.repeat(2))
// now all others
.replace(new RegExp(lookfor, 'g') /* global regex */, replace)
}
console.log(replaceFirstThenAll(
input,
" ",
"\n"
)) // Hello\n\nWorld\n !
Second Part
The way that you declare a string doesn't matter in js, so there's no way to replace it. No matter which method you use, the string is the same.
"String1" == 'String1' // true
'String2' == `String2` // true

regex match first occurrence of a single character [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'll make this extremely simple
I have a function that comes across a string as such by design: water 'thing one'
I need to split this string by the first space, since there are spaces elsewhere.
I've tried many regex expressions like / .*? /, but they can only match two consecutive spaces.
How do I do this?
Thanks in advance.
If you just want the portion of the string before and after the first space, you could use regex replacement here:
var input = "water 'thing one'";
var first = input.replace(/[ ].*$/, "");
var second = input.replace(/^\S*[ ]/, "");
console.log("first part: " + first);
console.log("second part: " + second);
You can capture them using String#match with this regex:
/(.*?) (.*)/
const text = "water 'thing one'";
const [, first, second] = text.match(/(.*?) (.*)/);
console.log('first:', first);
console.log('second:', second);

How to show first 2 characters and replace all the last by * [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm wondering how to show the first two characters and replace all last character of a string by symbol *.
Ex: 121,121,121 -> 12x,xxx,xxx .
Thanks
I love using regex when it comes to replace string according to some pattern.
var p = '121,121,121';
var regex = /(?<=.{2})([0-9])/gm;
console.log(p.replace(regex, 'x'));
You can use substring and regular expression. See the sample below.
var str = "121,121,121";
var res = str.substring(0, 2) + '' + str.substring(2, str.length).replace(/[0-9]/g,"x");
alert(res);
Just use substring and replace with a simple regex (to single out digits and keep commas and other punctuation):
const str = "121,121,121";
const obfuscated = `${str.substring(0, 2)}${str.substring(2).replace(/\d/g, "*")}`;
console.log(obfuscated);

Remove text between two parenthesis, if two more parenthesis [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have this string:
"This thing (123, 12) (2005.03 - 2011.12)"
I want to convert it to:
"This thing (2005.03 - 2011.12)"
Meaning remove text between two first parenthesis: (123, 12). But only if there are two or more parenthesis following in same string. So a string like
"Another thing (2005.05 - 2011.08)"
should be left as it is.
How can I do it with javascript?
You can use String.replace() with regex like this https://regex101.com/r/X7ioxu/1
var regex = /(\(.+?\))\s?\(/g;
var str1 = "This thing (123, 12) (2005.03 - 2011.12)";
var str1 = "This thing (2005.03 - 2011.12)";
alert(str1.replace(regex,'('));
alert(str2.replace(regex,'('));
With the data given, this works
var str = "This thing (123, 12) (2005.03 - 2011.12)";
var parts = str.split(/(?=\()/g); // split on ( with lookahead
if (parts.length==3) parts.splice(1,1)
str = parts.join("")
console.log(str)

Replace an expression in string using javascript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a string that has some numbers in the middle of the string.
For example,
var str = "abcd-123456.com"
I want to remove the numbers like this
abcd.com
I am not trying to replace all numbers.
I have to replace only -*. expression with "".
How do I do this in JavaScript?
var str = "abcd-123456.com"
str = str.replace(/-[0-9]*/g, '')
Your comments lead me to believe you want
var str = "abcd-123456.com";
var str1 = str.substring(0,str.indexOf("-"))+str.substring(str.indexOf("."))
console.log(str1);
//or with regex
// dasah plus 6 digits to nothing
var str2 = str.replace(/-\d{6}/,"")
console.log(str2);
// dash, digits and an dot to dot
var str3 = str.replace(/-\d+\./,".")
console.log(str3);
AS PER YOUR COMMENT
The answer of Shivaji Varma will nearly do the tricks.
var str = "abc5d-123456.c0om"
str = str.replace(/-[0-9]*./g, "")
console.log(str)
Soooo,
Using the slash indicate a regular expression.
[0-9] indicate you want to replace all digit between 0 and 9 (included)
"*" to remove all digits
"-" and "." to delimite

Categories