Cannot replace single emoji from a string [duplicate] - javascript

This question already has answers here:
Insert Unicode character into JavaScript
(6 answers)
Closed 2 years ago.
I have a string called completionBar which contains this:
let completionBar = `〚⬛⬛⬛⬛⬛〛`;
I'm trying to replace a single ⬛ with ⬜, so I tried:
completionBar.replace(/\U+2B1B/, 'U+2B1C');
but nothing happen, what I did wrong?

You can use /⬛/g in .replace().
I would try as the following if you want to replace all:
let completionBar = `〚⬛⬛⬛⬛⬛〛`;
const result = completionBar.replace(/⬛/g, '⬜');
console.log(result)
If you need only the first to replace:
let completionBar = `〚⬛⬛⬛⬛⬛〛`;
const result = completionBar.replace('⬛', '⬜');
console.log(result)
I hope this helps!

In my opinion, you can use escape and unescape function to show exactly the string code. It easy to debug and maintain the code.
let completionBar = `〚⬛⬛⬛⬛⬛〛`;
let escapeCompletionBar = escape(completionBar).replace(/u2B1B/g, 'u2B1C');
let result = unescape(escapeCompletionBar);

Related

How to use a regular expression in this case [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 2 months ago.
I want to check if a string-variable (called string) matches the following string: L-62000-64-000_0
My code looks like this:
let string = "L-62000-64-000_05641";
let part = 64;
const filter = new RegExp("^L-\d{5}-${part}-.+");
if (string.match(filter)) {
console.log("yes");
}
It is important, that the string starts with "L-". After this there should be a number with five digits and again a hyphen. The following two digits depend on a variable. After the variable there is again a hyphen. The rest of the string is not important for me. That's why I use the ".+"
The problem is, that this doesn't work and i don't know why...
if you want to use the part variable in the string, you need to use `` instead of ""
const filter = new RegExp(`^L-\d{5}-${part}-.+`);
You also need to add double \\ in this case
let string = 'L-62000-64-000_05641';
let part = 64;
const filter = new RegExp(`^L-\\d{5}-${part}-.+$`);
if (string.match(filter)) {
console.log("yes");
} else {
console.log("no");
}

Modifying a string with letters, parentheses and numbers [duplicate]

This question already has answers here:
Regular Expression to get a string between parentheses in Javascript
(10 answers)
Closed 4 years ago.
How can I modify this string:
"SRID=4326;POINT (-21.93038619999993 64.1444948)"
so it will return
"-21.93038619999993 64.1444948"
(and then I can split that)?
The numbers in the string can be different.
I've tried using .replace & split, but I couldn't get it to work properly. How can I make this happen using Javascript?
You can try with match and regex:
"SRID=4326;POINT (-21.93038619999993 64.1444948)".match(/\(([^)]+)\)/)[1]
// "-21.93038619999993 64.1444948"
I am not good using REGEXP but this could be a solution with pure split.
Hope it helps :>
var str = "SRID=4326;POINT (-21.93038619999993 64.1444948)" ;
var newStr = str.split('(')[1].split(')')[0];
console.log(newStr)
var new_string = string.replace("SRID=4326;POINT (", "");
You can use a regular expression. The first number is put in first, the second number is put in second.
const INPUT = "SRID=4326;POINT (-21.93038619999993 64.1444948)";
const REGEX = /SRID=\d+;POINT \((.+) (.+)\)/
const [_, first, second] = INPUT.match(REGEX);
console.log(first);
console.log(second);

How to check "#" tag is there or not in a string? [duplicate]

This question already has answers here:
How to tell if a string contains a certain character in JavaScript?
(21 answers)
Closed 4 years ago.
I'm a beginner in node.js so please do excuse me if my question is foolish. As we know we can use
var regex = /[ !##$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/g;
regex.test(str);
to check whether a string contains special charecters or not .But what I'm asking is how to check for only a particular charecter means how can I check only presence of #.
I tried to do
var regex = /[#]/g; regex.test(str).
Although it's not working but are there any other method of doing this?
You don't need a regex to find a single character in a string. You can use indexOf, like this:
var hasHashtag = str.indexOf('#') >= 0;
This returns true if the character is in the string.
Use includes to check the existence of # in your string. You don't actually require regex to do that.
var str = 'someSt#ring';
var res = str.includes('#');
console.log(res);
str = 'someSt#ri#ng';
res = str.includes('#');
console.log(res);
str = 'someString';
res = str.includes('#');
console.log(res);
Use indexOf
str.indexOf('#') >= 0;

can't get second number between parenthesis using regex [duplicate]

This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 5 years ago.
now I begin learning regex, and I have set of string in format like "(9/13)", and I need get second number. I try this regex: /\(.*?[^\d]*(\d+?)\)/g, in online regex it works normally.
But here:
var d = "(9/13)";
var v = /\(.*?[^\d]*(\d+?)\)/g;
alert(d.match(v));
it returns "(9/13)" , what am I doing wrong?
const source = "(9/13)";
const re = /\/(\d+)\)/;
console.log('result', re.exec(source).pop())
you can use Regex.exec() to find the number
const source = "(9/13)";
const re = /\(\d+\/(\d+)\)/;
console.log(re.exec(source)[1])

How to extract numbers from string in Javascript using regex [duplicate]

This question already has answers here:
Find and get only number in string
(4 answers)
Closed 8 years ago.
I have the following string
/Date(1317772800000)/
I want to use a Javascript regular expression to extract the numerical portion of it
1317772800000
How is this possible?
That should be it
var numPart = "/Date(1317772800000)/".match(/(\d+)/)[1];
No need for regex. Use .substring() function. In this case try:
var whatever = "/Date(1317772800000)/";
whatever = whatever.substring(6,whatever.length-2);
This'll do it for you: http://regex101.com/r/zR0wH4
var re = /\/Date\((\d{13})\)\//;
re.exec('/Date(1317772800000)/');
=> ["/Date(1317772800000)/", "1317772800000"]
If you don't care about matching the date portion of the string and just want extract the digits from any string, you can use this instead:
var re = /(\d+)/;
re.exec('/Date(1317772800000)/')
["1317772800000", "1317772800000"]

Categories

Resources