首页 \ 问答 \ Javascript:测试正则表达式并分配给变量,如果它在一行中匹配(Javascript: test regex and assign to variable if it matches in one line)

Javascript:测试正则表达式并分配给变量,如果它在一行中匹配(Javascript: test regex and assign to variable if it matches in one line)

要测试正则表达式是否匹配并将其分配给变量(如果不匹配),或者将其分配给某个默认值(如果不匹配),我目前正在执行以下操作:

var test = someString.match(/some_regex/gi);
var result = (test) ? test[0] : 'default_value';

我想知道是否有任何方法可以在JS中使用一行代码执行相同的操作。

澄清:我并不是想让我的代码更小,而是在我定义一些变量的地方使它更清晰:

var foo = 'bar',
    foo2 = 'bar2',
    foo_regex = %I want just one line here to test and assign a regex evaluation result%

To test if a regex matches and assign it to a variable if it does, or assign it to some default value if it doesn't, I am currently doing the following:

var test = someString.match(/some_regex/gi);
var result = (test) ? test[0] : 'default_value';

I was wondering if there is any way to do the same thing in JS with one line of code.

Clarification: I am not trying to make my code smaller, but rather make it cleaner in places where I am defining a number of variables like so:

var foo = 'bar',
    foo2 = 'bar2',
    foo_regex = %I want just one line here to test and assign a regex evaluation result%

原文:
更新时间:2022-02-23 09:02

最满意答案

那么,我会说一个调试器将是你最好的朋友。 我也有点惊讶收到的结果给你显示的打印输出,也许更多的问题没有显示。

查看以下行的位置: send(sSend, (char*)&num, sizeof(num), NULL);

并比较你接收到的地方: recv(sRecv, (char*)&num, sizeof(num), NULL);

提示,你打电话发送超过recv。


Well, I'd say a debugger will be your best friend. I'm also a little surprised the received end gave the printed output you show, perhaps more issues that aren't shown.

Look at the location of the following line: send(sSend, (char*)&num, sizeof(num), NULL);

And compare to where you have the receiving of that: recv(sRecv, (char*)&num, sizeof(num), NULL);

Hint, you call send more than recv.

相关问答

更多
  • 看看_SendComplete()事件 例如,使用表单级布尔值: Option Explicit Private mblnClosing As Boolean Private Sub Command1_Click() Winsock1.SendData "USERLEAVES" & txtUser.Text mblnClosing = True End Sub Private Sub Form_Load() mblnClosing = False End Sub Private Sub ...
  • 没有充分的理由不使用STL容器,因为它们是C ++的核心,因此它们将在每个C ++编译器中可用。 但是,要回答你的问题,如果你因任何原因需要避免它们,你可以这样做: int iResponseCapacity = MAX_BUFFER*5; int iResponseLength = 0; char *response = new char[iResponseCapacity]; do { iRecv = recv(m_socket, recvBuffer, MAX_BUFFER, 0); ...
  • 你需要在windows.h之前包含winsock2.h You need to include winsock2.h before windows.h
  • 你已经接到UDP的好东西有时会以任意方式重新排序消息。 总是发送包含所有可能信息的相同结构的解决方案几乎是正确的: 定义自己的基本消息,例如: struct msg_common{uint8_t sender, receiver; uint16_t message_id;}; // Take note that I used fixed-width types here! 并使所有特定消息以此开头,为每个消息保留公共前缀的非重叠范围。 您无法确认消息是否有效,这意味着: 它完整,又名。 UDP数据包包含整 ...
  • 我设法回答了! 我所做的是右击项目 - >属性 - >链接器 - >其他选项并在那里写-lws2_32。 我之前做的是项目 - >属性 - > c ++编译器 - >其他选项并在那里写-lws2_32。 I managed to answer it! What I did was right click on project -> properties -> linker -> Additional options and write -lws2_32 there. What I was doing bef ...
  • 数据可能需要多个recv呼叫才能完全接收。 发生这种情况时, Receive_Full_Packet会为每个此类调用分配一个新的缓冲区,并立即泄漏除其中一个之外的所有缓冲区。 它返回给调用者的是最后一个这样的分配 - 一个大多数未初始化的缓冲区,其中包含了随机垃圾,最后一块数据在最后。 Move isBuff = new char[Size]; 在循环外部,将它放在第一次调用调用大小的调用之后。 Finally Figured it out. Turns out there was absolutely n ...
  • 您假定在收到每个HTTP响应后连接保持打开状态,但不能保证。 您必须查看服务器的实际HTTP响应(特别是在状态行中报告的HTTP版本以及Connection标题处)以了解服务器是否将连接断开。 您正在发送HTTP 1.1请求,但不能保证服务器将回复HTTP 1.1响应。 如果响应使用HTTP 0.9,则在发送下一个HTTP请求之前,您必须关闭套接字并重新连接。 如果响应使用HTTP 1.0,并且Connection标头没有说明keep-alive ,那么在发送下一个HTTP请求之前,您必须关闭套接字并重新连 ...
  • 你没有分配足够的内存。 你的buffer大小只有1个char ( sizeof(char)是1个字节),但你试图填充12个字节( sizeof(Sid)是4个字节, sizeof(SLogin)是8个字节)。 即使你确实分配了足够的内存,它仍然无法工作,因为SLogin包含指向外部内存的指针,你没有将其序列化为可以安全发送/ SLogin的连续格式。 尝试更像这样的东西: #pragma pack(push, 1) // or equivilent for your compiler struct Sid ...
  • 这个: amountToSend = sprintf_s(serializedPacket, sizeof(buffer), (char*)&p); 根本没有任何意义。 sprintf_s()的第三个参数是格式化字符串,但是您将指针传递给结构,重新转换为字符指针。 这绝对没有意义。 将结构表示为文本字符串(序列化为字符串)可能是个好主意,但是您需要一个正确的格式化字符串: amountToSend = sprintf_s(serializedPacket, sizeof(buffer), "%d %s", ...
  • 那么,我会说一个调试器将是你最好的朋友。 我也有点惊讶收到的结果给你显示的打印输出,也许更多的问题没有显示。 查看以下行的位置: send(sSend, (char*)&num, sizeof(num), NULL); 并比较你接收到的地方: recv(sRecv, (char*)&num, sizeof(num), NULL); 提示,你打电话发送超过recv。 Well, I'd say a debugger will be your best friend. I'm also a little sur ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)