简体   繁体   English

在javascript中的两个标点字母之间替换表达式

[英]Replace expression between two punctuation letters in javascript

I have a string like the following, 我有一个类似以下的字符串,

var str = "abcd-12ad3dgs4g56.com"

I want like the following from this 我想要以下内容

abcd.com

I have to replace only -*. 我只需要替换-*. expression with . 用表示. .
How do I do this in JavaScript? 如何在JavaScript中执行此操作?

Simply try this 只需尝试一下

str.replace( /-\w+/, "" ); //"abcd.com"

  var str = "abcd-12ad3dgs4g56.com" console.log(str.replace(/-\\w+/, "")); 

You could use a regular expression with a positive lookahead. 您可以使用正向前瞻的正则表达式。

 var str = "abcd-12ad3dgs4g56.com"; console.log(str.replace(/-.*(?=\\.)/g, '')); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM